2010-12-21 76 views
14

我想要做的是,谷歌地圖上點擊移動標記

在我的地圖,當我點擊地圖上的某個地方,它顯示了點的標記,然後我在地圖上點擊不同點,然後它顯示另一個標記。但我希望它將第一個標記移到第二個點。

(我把「html標籤背後把代碼在這裏。)

這是我的代碼:

<html> 
<style type="text/css"> 
    #map_canvas { 
     height: 760px; 
     width: 1100px; 
     position: static; 
     top: 100px; 
     left: 200px; 
    } 
</style> 

<script type="text/javascript" 
     src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 

<script type="text/javascript"> 

    function initialize() { 
     var latlng = new google.maps.LatLng(42.55308, 9.140625); 

     var myOptions = { 
      zoom: 2, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      streetViewControl: false, 
      mapTypeControl: false, 
     }; 

     var map = new google.maps.Map(document.getElementById("map_canvas"), 
       myOptions); 


     google.maps.event.addListener(map, 'click', function(event) { 
      placeMarker(event.latLng); 
     }); 

     function placeMarker(location) { 

      var marker = new google.maps.Marker({ 
       position: location, 
       map: map 
       animation: google.maps.Animation.DROP, 

      }); 
      map.setCenter(location); 

     } 


    } 

</script> 
</head> 


<body onload="initialize()"> 
<div id="map_canvas" style="1500px; 1000px"></div> 
</body> 
</html> 
+0

您可以複製並粘貼到網頁的jsfiddle(http://jsfiddle.net/)我無法正確地讀出來,但是是你的「變種地圖的範圍「變量全球? – Jason 2010-12-21 22:44:50

回答

18

每次placemarker()是跑,它會創建一個新的標記

您需要在地標標記功能之外創建一次標記,然後在地標標記內部使用marker.setPosition()

1

要刪除一個標記只是setMap(null)它。

<html> 
<style type="text/css"> 
    #map_canvas { 
     height: 760px; 
     width: 1100px; 
     position: static; 
     top: 100px; 
     left: 200px; 
    } 
</style> 

<script type="text/javascript" 
     src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 

<script type="text/javascript"> 
    var oldMarker; 

    function initialize() { 
     var latlng = new google.maps.LatLng(42.55308, 9.140625); 

     var myOptions = { 
      zoom: 2, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      streetViewControl: false, 
      mapTypeControl: false, 
     }; 

     var map = new google.maps.Map(document.getElementById("map_canvas"), 
       myOptions); 


     google.maps.event.addListener(map, 'click', function(event) { 
      placeMarker(event.latLng); 
     }); 

     function placeMarker(location) { 

      marker = new google.maps.Marker({ 
       position: location, 
       map: map 
       animation: google.maps.Animation.DROP, 

      }); 

      if (oldMarker != undefined){ 
       oldMarker.setMap(null); 
      } 
      oldMarker = marker; 
      map.setCenter(location); 

     } 


    } 

</script> 
</head> 


<body onload="initialize()"> 
<div id="map_canvas" style="1500px; 1000px"></div> 
</body> 
</html> 
+2

+1或者他可以`setPosition(latlng:LatLng)`標記 – kjy112 2011-03-14 18:59:16

+0

這是真的。我讀了OP,並認爲我閱讀刪除標記,這是我心中唯一的東西:)顯然,這是最好的答案:) – AlfaTeK 2011-03-14 19:01:03

8

另一種解決方案是移動一個標記,因爲你只是用戶marker.setPosition()。 (感謝kjy112的提醒:)

<html> 
<style type="text/css"> 
    #map_canvas { 
     height: 760px; 
     width: 1100px; 
     position: static; 
     top: 100px; 
     left: 200px; 
    } 
</style> 

<script type="text/javascript" 
     src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 

<script type="text/javascript"> 
    var marker; 

    function initialize() { 
     var latlng = new google.maps.LatLng(42.55308, 9.140625); 

     var myOptions = { 
      zoom: 2, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      streetViewControl: false, 
      mapTypeControl: false, 
     }; 

     var map = new google.maps.Map(document.getElementById("map_canvas"), 
       myOptions); 


     google.maps.event.addListener(map, 'click', function(event) { 
      placeMarker(event.latLng); 
     }); 

     function placeMarker(location) { 



      if (marker == undefined){ 
       marker = new google.maps.Marker({ 
        position: location, 
        map: map, 
        animation: google.maps.Animation.DROP, 
       }); 
      } 
      else{ 
       marker.setPosition(location); 
      } 
      map.setCenter(location); 

     } 


    } 

</script> 
</head> 


<body onload="initialize()"> 
<div id="map_canvas" style="1500px; 1000px"></div> 
</body> 
</html>