如何使用Google Maps API路线计算每个国家的距离 [英] How to calculate distance for each country using Google Maps API Route

查看:144
本文介绍了如何使用Google Maps API路线计算每个国家的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个简单的路线从波兰华沙到德国柏林,开车。现在我可以计算所有路线的距离(572公里)。我如何计算每个国家的距离?德国〜100公里和波兰〜472公里。

我试图使用 start_location 来检测每个 DirectionsLeg legs [] 中,并通过 start_location 检测国家/地区名称。例如:

  getCountry(legs [0])=> 'pl'
getCountry(legs [1])=> 'pl'
getCountry(legs [2])=> 'pl'
//检测到边界
getCountry(legs [3])=> 'de'
getCountry(legs [4])=> 'de'

这是可行的,但是如果路线如此大, getCountry()返回 OVER_QUERY_LIMIT


$ b 已更新



感谢 Dr.Molle 寻求帮助!



工作小提琴
警告:代码正常工作,但未优化。小心!



对不起,我的英语

解决方案

您需要一种避免每个步骤检测国家请求的方法。



有一个包含国家边界的FusionTable: https://www.google.com/fusiontables/data?docid=16CTzhDWVwwqa0e5xe4dRxQ9yoyE1hVt_3ekDFQ#map:id=3

可能工作流程:


  1. 请求受该路线影响的国家/地区的几何图形。直接使用路径在这里不起作用,因为URL会太长,所以根据路线的边界过滤结果(它可能会比受影响的国家返回更多,但没有其他选择)



    路线warsaw-berlin的样本查询是:

      SELECT主权,json_4326 
    FROM 16CTzhDWVwwqa0e5xe4dRxQ9yoyE1hVt_3ekDFQ
    WHERE ST_INTERSECTS(kml_4326,
    RECTANGLE(LATLNG(51.88951,13.38593),
    LATLNG(52.52003,21.01219)))

    此查询中使用的RECTANGLE将基于界限 - 属性创建给定的路线。示例请求: http://jsfiddle.net/doktormolle/vexqe0ps/


  2. $ b
  3. $ b

  4. 循环路线的步骤,并且每个步骤遍历多边形数组。使用 google.maps.geometry.poly.containsLocation()来检查start_(或end_)位置是否放置在当前多边形内。如果是这样,请将当前步骤的距离添加到当前国家/地区的距离。


For example I have a simple rout From Warsaw, Poland To Berlin, Germany by car. Now I can calculate distance of all route (572km). How I can calculate distance for each country? Germany ~ 100km and Poland ~ 472km.

I tried to detect country border using start_location for each DirectionsLeg in legs[] and detect country name by start_location. For example:

getCountry(legs[0]) => 'pl'
getCountry(legs[1]) => 'pl'
getCountry(legs[2]) => 'pl'
//border detected
getCountry(legs[3]) => 'de'
getCountry(legs[4]) => 'de'

It's work, but if route is so "big", getCountry() return OVER_QUERY_LIMIT.

UPDATED

Thanks Dr.Molle for help!

Working fiddle. Warning: Code works fine, but it's not optimized. Be carefully!

P.S. Sorry for my English

解决方案

You'll need a way which avoids requests for each step to detect the country .

There is a FusionTable which contains the boundaries for countries: https://www.google.com/fusiontables/data?docid=16CTzhDWVwwqa0e5xe4dRxQ9yoyE1hVt_3ekDFQ#map:id=3

Possible workflow:

  1. request the geometries for the countries affected by the route. Using the path directly wouldn't work here, because the URL would be too long, so filter the results based on the bounds of the route(it may return more than the affected countries, but there is no other option)

    a sample-query for the route warsaw-berlin would be:

    SELECT sovereignt,json_4326 
    FROM 16CTzhDWVwwqa0e5xe4dRxQ9yoyE1hVt_3ekDFQ 
    WHERE ST_INTERSECTS(kml_4326,
                        RECTANGLE(LATLNG(51.88951,13.38593),
                                  LATLNG(52.52003,21.01219))) 
    

    The RECTANGLE used in this query will be created based on the bounds-property of the given route. Sample-request: http://jsfiddle.net/doktormolle/vexqe0ps/

  2. based on the returned geometries create an array of polygons(they must not be visible) for each country.

  3. Loop over the steps of the route, and for each step iterate over the polygon-array. Use google.maps.geometry.poly.containsLocation() to check if the start_(or end_)location is placed within the current polygon. When it does, add the distance of the current step to the distance for the current country.

这篇关于如何使用Google Maps API路线计算每个国家的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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