简体   繁体   English

如何检查Google Maps API中GRoute上的纬度/经度点?

[英]How to check if a Latitude/Longitude point is on the GRoute in Google Maps API?

I'm ploting a route between two points and I need to check if the route goes through a predefiend location. 我正在绘制两点之间的路线,并且需要检查路线是否经过预定位置。 In the api, each GStep has a start and end points but the path doesn't have to be straight. 在api中,每个GStep都有一个起点和终点,但是路径不必是直线。 Given a Lat/Long point, is there a way to check if it intersects with a route? 给定纬度/经度点,是否有办法检查其是否与路线相交?

You should be able to get the individual vertices of the polyline generated by GDirections very easily. 您应该能够很容易地获得由GDirections生成的折线的各个顶点。 First you have to make sure to pass the getPolyline: true option when you call the GDirections.load() if you do not attach the GDirections to the map. 首先,如果不将GDirections附加到地图上,则必须确保在调用GDirections.load()时传递getPolyline: true选项。 Then you can simply iterate through each vertex of the polyline, and then you should probably check if your point is within a certain distance threshold from your predefined location. 然后,您可以简单地遍历折线的每个顶点,然后可能应该检查点是否在距预定义位置一定距离的阈值内。

Consider the following example: 考虑以下示例:

var map = new GMap2(document.getElementById("map"));
var directions = new GDirections(map);

// Your predefined point
var pt = new GLatLng(51.470, -0.453);

directions.load('from: London, UK to: Glasgow, UK', { getPolyline: true });

GEvent.addListener(directions, "load", function() {
  for (var i = 0; i < directions.getPolyline().getVertexCount(); i++) {
    if (directions.getPolyline().getVertex(i).distanceFrom(pt) < 100) {
      // Your predefined point is within 100 meters of the route
    }
  }
});

You could probably improve this method by skipping some steps that are definitely too far from your test point. 您可以通过跳过某些绝对离测试点太远的步骤来改进此方法。 You could get the polyline index of each route step with the getPolylineIndex() method. 您可以使用getPolylineIndex()方法获取每个路线步骤的折线索引。 This can help you restricting your for loop to a narrower set of vertices. 这可以帮助您将for循环限制为一组较窄的顶点。

Further reading and reference: 进一步阅读和参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM