【问题标题】:Update Marker Position Google Maps V3更新标记位置 Google Maps V3
【发布时间】:2012-01-28 06:51:17
【问题描述】:

我是一个 javascript 的新手,目前我正在假装它,直到我让它大声笑,现在我遇到了一座小山,我正在努力克服:S.

目前我的脚本会找到用户的位置并在地图上添加一个图钉,同时将 LatLon 复制到某些表单字段。

除了放大用户位置之外,我还希望他们能够添加自定义地址,将其输入到文本字段中,进行地理编码,然后更新地图上的当前图钉。

这一切都有效,尽管它向地图添加了一个额外的 pin 而不是更新当前 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);
      }
    });

}

【问题讨论】:

标签: javascript google-maps-api-3


【解决方案1】:

要“移动”现有标记,您需要确保它是全局的,然后您可以在 findAddress 函数中更新其位置,如下所示:

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

【讨论】:

    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 2011-09-05
    • 2012-03-26
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多