admin管理员组文章数量:1026989
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())
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())
本文标签: jqueryCannot load google map javascript dynamicallyStack Overflow
版权声明:本文标题:jquery - Cannot load google map javascript dynamically - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745665552a2162154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论