Google Maps API将标记位置返回到隐藏字段

时间:2011-12-31 07:07:40

标签: javascript html google-maps

我试图让用户标记他们的位置,我使用下面的代码获取用户的邮政编码并放大他们的邻居但我无法弄清楚如何获得标记坐标。

我已经看到了一些像下面这样的actionlisteners的例子,但它没有用。  google.maps.event.addListener(         标记,         '拖动',         function(){             document.getElementById('lat')。value = marker.position.lat();             document.getElementById('lng')。value = marker.position.lng();         }     );

var localSearch;
var map;
var icon;
var addressMarkerOptions;
google.load("search", "1");
google.load("maps", "2");
google.setOnLoadCallback(initialize);

function initialize()
{
  localSearch = new GlocalSearch();
  icon = new GIcon(G_DEFAULT_ICON);
  addressMarkerOptions = { icon:icon , map: map ,draggable: true};
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  plotAddress("M13 0JN");
}

/**
* This takes either a postcode or an address string
*
*/
function plotAddress(address)
{
  localSearch.setSearchCompleteCallback(null, 
    function() {

      if (localSearch.results[0])
      {     
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        var marker = new GMarker(point, addressMarkerOptions);
        map.addOverlay(marker);
        map.setCenter(point, 15, G_NORMAL_MAP);
      }
      else
      {
        alert('address not found');
      }
    });

  localSearch.execute(address + ", UK");
}

2 个答案:

答案 0 :(得分:1)

document.getElementById('lat').value = marker.getLatLng().lat();
document.getElementById('lng').value = marker.getLatLng().lng();

答案 1 :(得分:0)

将向您展示如何执行此操作(以及更多内容)的优秀教程{@ 3}} ...

有关添加标记的信息,请参阅步骤8.

顺便说一下,您使用的是旧版本/现已弃用Google Maps API ...您应该升级到here