JQuery、Google 地图:“初始化之前无法调用 gmap 上的方法”

发布于 2024-11-25 08:33:51 字数 1666 浏览 0 评论 0原文

我有一个问题,我已经尝试解决了一段时间了。

有一段代码可以正常工作,但是如果我在其中添加一些 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-12-02 08:33:51

我也遇到了类似的问题,我所做的是将初始化函数创建为与其余代码分开的函数,当页面加载时,我调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文