【问题标题】:Google Maps API Geolocation + Lat / Lng谷歌地图 API 地理位置 + 纬度/经度
【发布时间】:2013-10-01 13:33:20
【问题描述】:

我想提供地理定位和可编辑的地图标记,通过 Google Maps API 向用户返回纬度/经度。我已经通过here 描述的方法成功地弄清楚了如何进行地理定位部分:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script>
function success(position) {
  var mapcanvas = document.createElement('div');
  mapcanvas.id = 'mapcontainer';
  mapcanvas.style.height = '400px';
  mapcanvas.style.width = '600px';

  document.querySelector('article').appendChild(mapcanvas);

  coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

  var options = {
    zoom: 15,
    center: coords,
    mapTypeControl: false,
    navigationControlOptions: {
        style: google.maps.NavigationControlStyle.SMALL
    },
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("mapcontainer"), options);

  var marker = new google.maps.Marker({
      position: coords,
      map: map,
      title:"You are here!"
  });
}

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success);
} else {
  error('Geo Location is not supported');
}
</script>

但是,我还希望能够允许用户移动地图并通过地图标记选择另一个位置,并捕获他们选择的纬度/经度。 described here 方法可以满足我的要求,但我不确定如何让两个脚本一起工作以添加我需要的功能。如果这有助于解释目标,我正在尝试创建与 über 类似的东西。

如何制作以地理位置为中心的谷歌地图,而且其地图标记可以移动并记录最终标记的纬度/经度?谢谢你的想法!

【问题讨论】:

标签: javascript google-maps google-maps-api-3 geolocation geopositioning


【解决方案1】:

您要先在标记选项中将标记的draggable 属性设置为true

然后在标记上监听 dragend 事件 - 您可以使用 google.maps.event.addListener(marker, 'dragend', callback); 进行设置。

然后在您的callback 函数中,使用getPosition() 方法获取标记的位置。

如@geocodezip 所述,请参阅API documention 以获得更多参考。

【讨论】:

    猜你喜欢
    • 2018-07-26
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多