简体   繁体   English

如何在谷歌地图中创建移动标记

[英]How to create a moving marker in google maps

I a using Google Maps in my app. 我在我的应用中使用Google Maps。

The user is to be able to place a marker on any place in the map. 用户将能够在地图上的任何地方放置标记。

To this end I wrote the following code: 为此,我编写了以下代码:

var marker;

function myMap() {

  var mapCanvas = document.getElementById("map-canvas");
  var myCenter=new google.maps.LatLng(50.833,-12.9167);
  var mapOptions = {center: myCenter, zoom: 5};
  var map = new google.maps.Map(mapCanvas, mapOptions);

  google.maps.event.addListener(map, 'click', function(event) {

    //marker.setMap(null); // this line does not work
    placeMarker(map, event.latLng);
  });
}

function placeMarker(map, location) {

    marker = new google.maps.Marker({
      position: location,
      map: map
    });

}

The marker is supposed to always move to the place where the user clicked. 该标记应该始终移动到用户单击的位置。 The line 线

marker.setMap(null); 

is supposed to remove the old marker (before the new marker is placed). 应该删除旧标记(在放置新标记之前)。 However, with this line in the code I cannot place any markers any more. 但是,在代码的这一行中,我无法再放置任何标记。 Not including this line means that every marker stays in the map and is not removed (ie the map is filling up with markers over time). 不包括这条线意味着每个标记都留在地图中并且不会被删除(即随着时间的推移,地图上充满了标记)。

The problem is that you try to use method setMap after the first click when marker variable doesn't have this method. 问题是,当marker变量没有此方法时,您会在第一次单击后尝试使用setMap方法。 So, first check if marker has the method and then call it. 因此,首先检查marker是否具有方法,然后调用它。

google.maps.event.addListener(map, 'click', function(event) {

        // check if setMap is available and call it.
        if(marker.hasOwnProperty('setMap')){
            marker.setMap(null);
        }
        placeMarker(map, event.latLng);

});

Look at the javascript console, you will see Uncaught TypeError: Cannot read property 'setMap' of undefined . 查看JavaScript控制台,您将看到Uncaught TypeError: Cannot read property 'setMap' of undefined The first time, marker is null , you need to only set its map property to null if it already exists. 第一次,marker为null ,如果它的map属性已经存在,则只需将其map属性设置为null

google.maps.event.addListener(map, 'click', function(event) {

  if (marker) marker.setMap(null);
  placeMarker(map, event.latLng);
});

proof of concept fiddle 概念证明

code snippet: 代码段:

 var marker; function myMap() { var mapCanvas = document.getElementById("map-canvas"); var myCenter = new google.maps.LatLng(50.833, -12.9167); var mapOptions = { center: myCenter, zoom: 5 }; var map = new google.maps.Map(mapCanvas, mapOptions); google.maps.event.addListener(map, 'click', function(event) { if (marker) marker.setMap(null); placeMarker(map, event.latLng); }); } function placeMarker(map, location) { marker = new google.maps.Marker({ position: location, map: map }); } google.maps.event.addDomListener(window, "load", myMap); 
 html, body, #map-canvas { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map-canvas"></div> 

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

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