根据给定角度获取圆形点的LatLong

时间:2017-10-02 12:58:16

标签: javascript google-maps

鉴于你在地图上定义了这个圆圈,你将如何获得以给定角度相交的点的LatLong?

var degrees = 90;
var center = {lat: 34.052235, lng: -118.243683};
var circle = new google.maps.Circle({
    center: center,
    radius: 500000
});

var pointOnCircle = ?

Map

1 个答案:

答案 0 :(得分:1)

您可以使用geometry library computeOffset方法:

  var pointOnCircle = google.maps.geometry.spherical.computeOffset(circle.getCenter(), circle.getRadius(), degrees);

proof of concept fiddle

screen shot of result

代码段



function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var degrees = 90;
  var center = {
    lat: 34.052235,
    lng: -118.243683
  };
  var circle = new google.maps.Circle({
    center: center,
    radius: 500000,
    map: map
  });
  map.fitBounds(circle.getBounds());
  var pointOnCircle = google.maps.geometry.spherical.computeOffset(circle.getCenter(), circle.getRadius(), degrees);
  var marker = new google.maps.Marker({
    map: map,
    position: pointOnCircle
  });
  var polyline = new google.maps.Polyline({
    map: map,
    path: [circle.getCenter(), pointOnCircle]
  })
}
google.maps.event.addDomListener(window, "load", initialize);

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

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