如何在Google Maps API中的GRoute上检查纬度/经度点? [英] How to check if a Latitude/Longitude point is on the GRoute in Google Maps API?

查看:67
本文介绍了如何在Google Maps API中的GRoute上检查纬度/经度点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制两点之间的路线,因此我需要检查路线是否经过预定位置.在api中,每个 GStep 都有一个起点和终点,但路径不必一定是直的.给定纬度/经度点,是否有办法检查其是否与路线相交?

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. 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. 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. 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.

请考虑以下示例:

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. This can help you restricting your for loop to a narrower set of vertices.

进一步阅读和参考:

  • GDirections
  • GRoute
  • GStep

这篇关于如何在Google Maps API中的GRoute上检查纬度/经度点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆