在给定LatLng位置和方位的情况下计算多边形边缘上的位置

时间:2017-04-24 16:41:47

标签: javascript java google-maps google-maps-api-3 gis

我有一个LatLng位置(用户的当前位置)和一个方位(从多边形的中心)。我知道位置当前位于多边形内部,我可以从多边形的中心和用户的位置获得方位。

我需要知道多边形边缘上的点,从中心给出轴承。我设想这是一个来自中心和多边形边缘的线之间的交集,但我没有在Google Maps API或任何在线示例中看到任何帮助。

这将使用Google Maps SDK以Java编写,但我希望使用任何语言的解决方案,尤其是Javascript。

以下粗略绘制的图表(希望)更清楚地说明了这个问题。

Polygon with user location and centre

这是一个带有(不是)中心位置和用户位置的jsfiddle。

http://jsfiddle.net/joshkopecek/rh9mzyoa/2/

function initialize() {
  var map = new google.maps.Map(document.getElementById("map"), {
    zoom: 4,
    center: new google.maps.LatLng(22.7964, 79.8456),
    mapTypeId: google.maps.MapTypeId.HYBRID
  });

  var coords = [
    new google.maps.LatLng(18.979026, 72.949219), //Mumbai
    new google.maps.LatLng(28.613459, 77.255859), //Delhi
    new google.maps.LatLng(22.512557, 88.417969), //Kolkata
    new google.maps.LatLng(12.940322, 77.607422), //Bengaluru
  ];

  metros = new google.maps.Polygon({
    paths: coords,
    strokeColor: "#0000FF",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#0000FF",
    fillOpacity: 0.26
  });

  userPos = new google.maps.LatLng(21.1952534, 81.2680344);
  userMarker = new google.maps.Marker({
    position: userPos,
    map: map
  });

  centrePos = new google.maps.LatLng(22.049832, 78.9022019);

  centreMarker = new google.maps.Marker({
    position: centrePos,
    map: map
  });

  heading = google.maps.geometry.spherical.computeHeading(centrePos, userPos);

  metros.setMap(map);

  var metros;
  var userMarker;
  var centreMarker;
  var heading;
}

1 个答案:

答案 0 :(得分:2)

According to the docs you could use isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number) which will return if a given point is on the edge of a polygon, within a certain tolerance. You could use computeOffset with your heading to test points along a path until you find what you need.

As an aside, any real/intensive spatial analysis I have not had much luck with client-side technologies. My preferred method would usually be a server call backed by a spatial database like PostGIS that I can then offload all the analysis to. You could look at ESRI's JavaScript api though, if you were so inclined.