The follow tells me that GMap2 is not defined. But the code that uses GMap2 is in the callback.
$(function() {
$('#sample').click(function() {
$.getScript(";amp;v=2&sensor=true&key=API_KEY_HERE", function() {
var map = new GMap2(document.getElementById("mapTest"));
map.setCenter(new GLatLng(18, -77.4), 13);
map.setUIToDefault();
});
});
});
<a id="sample">Click Me</a>
<div id="mapTest" style="width: 200px; height: 100px;"></div>
The follow tells me that GMap2 is not defined. But the code that uses GMap2 is in the callback.
$(function() {
$('#sample').click(function() {
$.getScript("http://maps.google./maps?file=api&v=2&sensor=true&key=API_KEY_HERE", function() {
var map = new GMap2(document.getElementById("mapTest"));
map.setCenter(new GLatLng(18, -77.4), 13);
map.setUIToDefault();
});
});
});
<a id="sample">Click Me</a>
<div id="mapTest" style="width: 200px; height: 100px;"></div>
Share
Improve this question
edited Jan 8, 2010 at 5:11
Doug Neiner
66.3k14 gold badges110 silver badges118 bronze badges
asked Jan 8, 2010 at 4:51
Shawn McleanShawn Mclean
57.5k96 gold badges281 silver badges412 bronze badges
1
- 1 Yes, the OP has supplied an API key. I have removed it from the question and replaced it with API_KEY_HERE – Doug Neiner Commented Jan 8, 2010 at 5:11
2 Answers
Reset to default 5You can go two ways with this:
1. Continue to use $.getScript
:
It seems that you need both an async=2
parameter as well as a different callback structure for it to work. My answer is adapted to your code from this great walkthrough here.
<script type="text/javascript">
function map_callback(){
var map = new GMap2(document.getElementById("mapTest"));
map.setCenter(new GLatLng(18, -77.4), 13);
map.setUIToDefault();
}
$(function(){
$('#sample').click(function(){
$.getScript("http://maps.google./maps?file=api&v=2&sensor=true&callback=map_callback&async=2&key=API_KEY_HERE");
}
}
</script>
2. Use the Google AJAX Loader
Since you are already using a Google Library, why not use their loader to help you out:
<script type="text/javascript" src="http://www.google./jsapi?key=ABCDEFG"></script>
<script type="text/javascript">
google.load("jquery", "1.3.2");
google.setOnLoadCallback(function(){
$('#sample').click(function(){
google.load("maps", "2", {"callback" : function(){
var map = new GMap2(document.getElementById("mapTest"));
map.setCenter(new GLatLng(18, -77.4), 13);
map.setUIToDefault();
} });
});
}, true); // Passing true, though undocumented, is supposed to work like jQuery DOM ready
</script>
Did you check the if the browser is patible? I do this in all my GMAP applications, although it rarely fails...
if (GBrowserIsCompatible())
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745665554a4639100.html
评论列表(0条)