2011-12-27 107 views
13

我是一個相當新手的JavaScript的東西,我現在僞造它,直到我做它大聲笑,現在我想碰到一個小山,我努力克服:S。更新標記位置谷歌地圖V3

當前我的腳本查找用戶位置,並在將LatLon複製到某些表單域時將一個PIN添加到地圖。

除了放大用戶位置之外,我希望他們能夠添加一個自定義地址,並將其輸入到文本字段中,進行地理編碼,然後更新地圖上的當前引腳。

這一切都能正常工作,儘管它在地圖上增加了一個別針,而不是更新當前的針腳。

我不確定如何將地址地理編碼功能的值傳遞迴原始引腳/或者我刪除原始引腳並添加新引腳。我敢肯定,我可以重複使用某些功能,以及...我不認爲我的代碼是非常有效的:/

什麼辦法,我希望一個大師在那裏可以幫助我走出

乾杯 尼克

var geocoder; 
var map; 
var pos; 


function initialize() { 

geocoder = new google.maps.Geocoder(); 
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687); 
var address = document.getElementById("address").value; 
var initialLocation; 

var myOptions = { 
    zoom: 12, 
    center: initialLocation, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
} 


// Try HTML5 geolocation 
    if(navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, 
             position.coords.longitude); 


     var marker = new google.maps.Marker({ 
      map: map, 
      position: pos, 
      title: 'Location found using HTML5.', 
      draggable: true 
     }); 



     var lat = position.coords.latitude 
     var lng = position.coords.longitude 
     document.getElementById('geo_latitude').value=lat; 
     document.getElementById('geo_longitude').value=lng; 

     google.maps.event.addListener(marker, "dragend", function(event) { 

      var lat = event.latLng.lat() 
      var lng = event.latLng.lng() 

      var infowindow = new google.maps.InfoWindow({ 
       content: '<b><?php _e('Latitude:');?></b>' + lat + '<br><b><?php _e('Longitude:');?></b>' + lng 
      }); 
      infowindow.open(map, marker); 

      google.maps.event.addListener(marker, "dragstart", function() { 

       infowindow.close(); 
      }); 

     document.getElementById('geo_latitude').value=lat; 
     document.getElementById('geo_longitude').value=lng; 


     }); 


     map.setCenter(pos); 
     }, function() { 
     handleNoGeolocation(true); 
     }); 

    } else if (google.gears) { 
     browserSupportFlag = true; 
     var geo = google.gears.factory.create('beta.geolocation'); 
     geo.getCurrentPosition(function(position) { 
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude); 
      map.setCenter(initialLocation); 
     }, function() { 
      handleNoGeoLocation(browserSupportFlag); 
     }); 
     // Browser doesn't support Geolocation 


     } else { 
     browserSupportFlag = false; 
     handleNoGeolocation(browserSupportFlag); 
     } 


     function handleNoGeolocation(errorFlag) { 
     if (errorFlag == true) { 
      alert("Geolocation service failed."); 
      initialLocation = newyork; 
     } else { 
      alert("Your browser doesn't support geolocation. We've placed you in New York."); 
      initialLocation = newyork; 
     } 
     map.setCenter(initialLocation); 
     } 


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

    //-------------------------------------------------------End initialize 

    function findAddress(address) { 

var address = document.getElementById("address").value; 

    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var pos = results[0].geometry.location; 

     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 

} 
+0

的http:// stacko verflow.com/questions/6190482/google-maps-api-v3-update-marker – 2011-12-27 04:05:27

回答

31

要「移動」您現有的標記,你會想要確保其在全球,然後你可以用類似更新其findAddress功能中的位置:

marker.setPosition(results[0].geometry.location);