Google Maps API v3标记有时只会加载

时间:2017-03-02 21:22:01

标签: javascript ruby-on-rails google-maps-api-3 google-maps-markers ruby-on-rails-2

我有一个使用Google Maps API V3构建的地图制作工具。它允许用户输入两个或多个位置并生成地图和路线。我还有一个复选框,当点击时,会显示指示附近兴趣点的标记。

当我第一次构建该工具时,我认为它每次都运行良好。不过,最近我注意到点击复选框时并不总是出现标记。地图和路由工作正常,但标记只能偶尔使用。这个错误似乎发生在他们不工作时:

Uncaught ReferenceError: map is not defined

它引用了" cmarkers"的一部分。 javascript部分(见下文)。

背景细节:这是Rails网络应用程序和名为"行程"的网页/布局的一部分。当您登陆行程网页并点击"地图制作工具"图标,出现地图制作工具。它被加载到i-frame中,它被称为" map.html.erb",地图视图位于/ views / itineraries中。但是,地图制作者的所有javascript都存在于Itineraries布局文件中。

基于对这些帖子的回顾,我认为这可能是我订购或初始化代码的方式,我认为主要的罪魁祸首可能在于" cmarkers"代码部分。

我尝试了几种不同的更改,但每种更改都没有工作或停止了初始化地图。这是javascript;请注意,API密钥和其他小部分将被编辑。下面是标记的代码。

        <script src="https://maps.googleapis.com/maps/api/js?key=MYAPIKEY&sensor=false"></script>
    <script type='text/javascript'> 
    $(function(){
        var directionsDisplay;
        var map;            

        function overlaysClear() {
          if (markersArray) {
            for( var i = 0, n = markersArray.length; i < n; ++i ) {
              markersArray[i].setVisible(false);
            }
          }
        }

        function overlaysShow() {
          if (markersArray) {
            for( var i = 0, n = markersArray.length; i < n; ++i ) {
              markersArray[i].setVisible(true);
            }
          }
        }

        $("#showmapview").click(function() {
        overlaysClear();
        $('#mapeach').attr('checked', false);
        });

        $('#mapeach').change(function() {
         if( $('#mapeach').attr("checked")) {
                  overlaysShow();
                  }
                  else  {
                  overlaysClear();
                  }
        });
        cmarkers();

        google.maps.event.addDomListener(window, 'load', initialize); 
    });

    var directionsService = new google.maps.DirectionsService();
        var markersArray = [];
        var arrInfoWindows = null;

    function initialize() {
          var rendererOptions = {
          draggable: true,
          panel:document.getElementById('directions_panel')
         };

          directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
          var chicago = new google.maps.LatLng(41.850033, -87.6500523);
              var mapOptions = {
              zoom: 6,
              center: chicago,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
          map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

          directionsDisplay.setMap(map);
        }

        function calcRoute() {

            var start = document.getElementById("start").value;
            var end = document.getElementById("end").value;
            var waypts = [];
          var checkboxArray = document.getElementById("waypoints");
          for (var i = 0; i < checkboxArray.length; i++) {
             waypts.push({
                  location:checkboxArray[i].value,
                  stopover:true
              });
         }

          var request = {
              origin: start,
              destination: end,
              waypoints: waypts,
              optimizeWaypoints: optimize,
              travelMode: google.maps.TravelMode.DRIVING
          };
          directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
              directionsDisplay.setDirections(response);
              var route = response.routes[0];
            }    
          });
        }; 

        function cmarkers() {
                    $.getJSON( "/mapeach.js", {}, function( data ) {
                    $.each( data, function( i, item ) {
                    var loc = item.mainlocation;
                    $("#markers").append('<li><a href="#" rel="' + i + '">' + loc.nickname + '</a></li>');
                    var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(+loc.latitude, +loc.longitude),
                    map: map,
                    title: loc.nickname,
                    });
                    markersArray.push(marker);

                      var infowindow = new google.maps.InfoWindow({
                            content: '<a class="clink" href="/spots/'+ loc.id +'/'+ loc.nickname +'" target="_blank">'+ loc.nickname +'</a>'
                            });

                    google.maps.event.addListener(marker, 'click', function() {
                         if (arrInfoWindows != null) { 
                            arrInfoWindows.close(); 
                        } 
                        infowindow.open(map,marker);
                        arrInfoWindows = infowindow;
                        });
                      });
                    });
                };  
    </script>

mapeach.js文件的格式如下:

[{"mainlocation":{"latitude":"40.706352","nickname":"First Location","id":100000,"longitude":"-73.987650"}},{"mainlocation":{"latitude":"34.061148","nickname":"Second Location","id":100001,"longitude":"-118.273067"}}]

1 个答案:

答案 0 :(得分:1)

我能够通过在初始化中移动cmarkers代码来解决这个问题。我认为javascript没有明确初始化中的map变量也是cmarkers函数中的map变量(对不起,如果语言不精确;我在js中不是很好)。见下文:

function initialize() {
              var rendererOptions = {
              draggable: true,
              panel:document.getElementById('directions_panel')
             };

          directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
          var chicago = new google.maps.LatLng(41.850033, -87.6500523);
              var mapOptions = {
              zoom: 6,
              center: chicago,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
          map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

          directionsDisplay.setMap(map);

          cmarkers();
          function cmarkers() {
                $.getJSON( "/mapeach.js", {}, function( data ) {
                $.each( data, function( i, item ) {
                var loc = item.mainlocation;
                $("#markers").append('<li><a href="#" rel="' + i + '">' + loc.nickname + '</a></li>');
                var marker = new google.maps.Marker({
                position: new google.maps.LatLng(+loc.latitude, +loc.longitude),
                map: map,
                title: loc.nickname,
                });
                markersArray.push(marker);

                  var infowindow = new google.maps.InfoWindow({
                        content: '<a class="clink" href="/spots/'+ loc.id +'/'+ loc.nickname +'" target="_blank">'+ loc.nickname +'</a>'
                        });

                google.maps.event.addListener(marker, 'click', function() {
                     if (arrInfoWindows != null) { 
                        arrInfoWindows.close(); 
                    } 
                    infowindow.open(map,marker);
                    arrInfoWindows = infowindow;
                    });
                  });
                });
            };  

这篇文章特别赞扬我的想法:can't see google marker