JQuery、Google 地图:“初始化之前无法调用 gmap 上的方法”
我有一个问题,我已经尝试解决了一段时间了。
有一段代码可以正常工作,但是如果我在其中添加一些 JQuery,它就会使所有内容崩溃。
我收到以下错误:“Uncaught无法在初始化之前调用gmap上的方法;尝试调用方法'addMarker”
错误代码从“$.each(markersList.markers”开始,
任何帮助指出我正确方向的帮助都会不胜感激!
function initialize() {
var myOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
$.each(markersList.markers,
function(map, marker) {
var destination = (marker.lat + ', ' + marker.lng);
$('#map_canvas').gmap('addMarker',
{ 'position': new google.maps.LatLng(marker.lat, marker.lng), 'title': marker.title }, function(map, marker) {
$('#map_canvas').gmap('addInfoWindow',
{ 'content': null }, function(iw) {
$(marker).click(function() {iw.open(map, marker);
map.panTo(marker.getPosition());
$('#to').val(destination);
alert("You've selected " + marker.title + " as your destination. Please enter the origin");
});
});
});
});
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
contentString = "Your location was found and has been added to starting point.";
map.setCenter(initialLocation);
$('#from').val(initialLocation);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
I have a problem I've been trying to figure out for a while now.
There's a block of code that works fine, but then if I add some JQuery in it, it crash everything.
I get the following error : "Uncaught cannot call methods on gmap prior to initialization; attempted to call method 'addMarker"
The faulty code starts from "$.each(markersList.markers,"
Any help to point me out in the right direction would be greatly appreciated. Thanks !
function initialize() {
var myOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
$.each(markersList.markers,
function(map, marker) {
var destination = (marker.lat + ', ' + marker.lng);
$('#map_canvas').gmap('addMarker',
{ 'position': new google.maps.LatLng(marker.lat, marker.lng), 'title': marker.title }, function(map, marker) {
$('#map_canvas').gmap('addInfoWindow',
{ 'content': null }, function(iw) {
$(marker).click(function() {iw.open(map, marker);
map.panTo(marker.getPosition());
$('#to').val(destination);
alert("You've selected " + marker.title + " as your destination. Please enter the origin");
});
});
});
});
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
contentString = "Your location was found and has been added to starting point.";
map.setCenter(initialLocation);
$('#from').val(initialLocation);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到了类似的问题,我所做的是将初始化函数创建为与其余代码分开的函数,当页面加载时,我调用 Initialize() ,并在函数末尾调用 searchLocations() ,它添加了地图上的所有标记。
I had a similar issue with mine, what I did was create the initialize function as a separate function from the rest of the code, when the page loads I call Initialize() and at the end of the function I call searchLocations() which adds all of the markers to the map.