简体   繁体   English

在点击事件中加载Google Map API,标记不会显示

[英]Load google map API on click event, the marker doesn't show

I am trying to load different Google map instances based on clicking different addresses. 我正在尝试根据不同的地址加载不同的Google地图实例。 The event listener works as a charm, but the problem is the Marker doesn't load. 事件侦听器具有吸引力,但问题是Marker无法加载。 Here is my code. 这是我的代码。 I would like the Marker loads for each address onclick event. 我希望标记为每个地址onclick事件加载。

<div style="float:left;width:100%;">
  <div style="float:left;">
  <h2>Locations</h2>
  <ul class="menu">
   <li onclick="loadMap('43.6581859','-79.3906945');">
     <a href="#">Category 1</a></li>
   <li onclick="loadMap('43.658589','-79.3872499');">
     <a href="#">Category 2</a></li>
   <li onclick="loadMap('43.6533033','-79.4058543');">
     <a href="#">Category 3</a></li>
  </div>
  <div style="float:left; width: 20px;">&nbsp;</div>
  <div style="width:400px;height:450px; overflow:hidden;"><h2>Map</h2>
  <div id="map_canvas" style="height:400px;"></div>
  </div>
</div>
<!--Load Map Script -->
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
  var map;
  var home;
  var markersArray = [];

  function initialize() {
       home = new google.maps.LatLng('43.659924','-79.38875');   
       var opts = {
            zoom: 15,
            center: home,
            mapTypeId: google.maps.MapTypeId.ROADMAP
       };

       map = new google.maps.Map(document.getElementById('map_canvas'), opts);
       google.maps.event.addListener(map, "click", function(event) {
              showMarker(event.latLng);
           });    

  }

  function loadMap(lat,lng)
       {
           var location= new google.maps.LatLng(lat, lng);
           map.setCenter(location);

       }

    function showMarker(location) {    
           deleteOverlays();               
           var marker = new google.maps.Marker({
               position: location,
               map: map
           });
           markersArray.push(marker);

    }


   function deleteOverlays() {
        if (markersArray) {
            for (i=0; i < markersArray.length; i++) {
                markersArray[i].setMap(map);
            }
        markersArray.length = 0;
        }
    }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

Any help is much appreciated! 任何帮助深表感谢!

You're calling the showMarker on click of the google map, instead of on click of the list items. 您是在单击Google地图时调用showMarker,而不是在单击列表项时调用。

If you call showMarker(location) in the loadMap function it should work. 如果在loadMap函数中调用showMarker(location),则它应该可以工作。

function loadMap(lat,lng) {
    var location = new google.maps.LatLng(lat, lng);
    map.setCenter(location);
    showMarker(location);
} 

And you can remove the click event listener on the map (unless you want it for some other reason). 而且,您可以在地图上删除click事件监听器(除非您出于某些其他原因而想要)。

You also seem to have a problem with the deleteOverlays function, if you're trying to delete all the existing markers you should pass null into the setMap function: 您似乎还对deleteOverlays函数有问题,如果要删除所有现有标记,则应将null传递给setMap函数:

function deleteOverlays() {
    if (markersArray) {
        for (i=0; i < markersArray.length; i++) {
            markersArray[i].setMap(null); //pass null in here
        }
    markersArray.length = 0;
    }
}

If you want to show marker after address selection, you should move showMarker(location) in the loadMap. 如果要在选择地址后显示标记,则应在loadMap中移动showMarker(location)。

function loadMap(lat,lng)
       {
           var location= new google.maps.LatLng(lat, lng);
           map.setCenter(location);
           showMarker(location);
       }

Also i suggest to move 'onclick' event from <li> element to inner <a> element. 我也建议将'onclick'事件从<li>元素移动到内部<a>元素。 This will avoid accidental clicks. 这样可以避免意外点击。

   <li><a onclick="loadMap('43.6581859','-79.3906945');" href="#">Category 1</a></li>
   <li><a onclick="loadMap('43.658589','-79.3872499');" href="#">Category 2</a></li>
   <li><a onclick="loadMap('43.6533033','-79.4058543');" href="#">Category 3</a></li>

Also I wnat to note, that your deleteOverlays() function not working properly, because in your example you call 我还要注意,您的deleteOverlays()函数无法正常工作,因为在您的示例中,您调用了

markersArray[i].setMap(map);

instead 代替

markersArray[i].setMap(null);

I think it should be better :) 我认为应该更好:)

I've created working JSFiddle for you with yor code. 我已经用您的代码为您创建了工作的JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM