如何获得谷歌地图标记左右拉特朗和距离

时间:2018-05-30 12:07:12

标签: google-maps google-maps-api-3 google-maps-markers

如何左右lat lng以及与谷歌地图标记点的距离请找到下面的图像以供参考。 注意:从标记点开始我只需要左右latlng距离应该在100m左右,100m也不固定它可以改变10m或50m在未来。是否可以使用谷歌地图API V3来做到这一点。 enter image description here

1 个答案:

答案 0 :(得分:1)

使用几何库的computeOffset方法:

var markE = new google.maps.Marker({
  position: google.maps.geometry.spherical.computeOffset(map.getCenter(), distance, 90),
  map: map,
  label: "E"
})
var markW = new google.maps.Marker({
  position: google.maps.geometry.spherical.computeOffset(map.getCenter(), distance, -90),
  map: map,
  label: "W"
})

proof of concept fiddle

screenshot of resulting map

代码段



var map;
var distance = 100; // meters
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: 13.025228,
      lng: 80.257518
    },
    zoom: 18
  });
  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map
  });
  var markE = new google.maps.Marker({
    position: google.maps.geometry.spherical.computeOffset(map.getCenter(), distance, 90),
    map: map,
    label: "E"
  })
  var markW = new google.maps.Marker({
    position: google.maps.geometry.spherical.computeOffset(map.getCenter(), distance, -90),
    map: map,
    label: "W"
  })
}
google.maps.event.addDomListener(window, "load", initMap);

html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="map"></div>
&#13;
&#13;
&#13;