更新谷歌地图V3标记位置

20

我对javascript方面的知识还很浅显,目前只能在模仿中学习。我遇到了一个小问题,一时无法解决。

目前我的脚本可以找到用户的位置并在地图上添加一个标记,同时将经纬度复制到一些表单字段中。

除了仅仅缩放到用户位置外,我希望用户能够在文本字段中输入自定义地址,并进行地理编码,然后更新地图上的当前标记。

这一切都有效果了,但它会在地图上添加一个额外的标记而不是更新当前标记。

我不确定如何将地址地理编码函数的值传回原始标记/或者删除原始标记并添加新标记。我相信我也可以重复使用一些函数......我不认为我的代码效率特别高 :/。

不管怎么样,我希望有位大师能够帮助我。

谢谢, Nick

 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);
      }
    });

}

https://dev59.com/OG025IYBdhLWcg3wJCVD - Sudhir Bastakoti
1个回答

43

要“移动”现有标记,您需要确保它是全局的,然后可以在findAddress函数中使用类似以下内容更新其位置:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接