简体   繁体   English

如何使用Google Maps Roads API使用坐标列表来计算距离

[英]How to calculate distance using lists of coordinates with Google Maps Roads API

I'm generating static map with snapToRoads (since it accepts multiple waypoints). 我正在使用snapToRoads生成静态地图(因为它接受多个航路点)。

Given I have lists of coordinates, how can I calculate distance between those coordinates and sum it to get total driving distance . 给定我有坐标的列表,如何计算这些坐标之间的距离并将其求和以获得总行驶距离

Or is there other better way to do it in ruby code ? 还是有其他更好的方法在Ruby代码中做到这一点?

Sample snap-to-roads request: 快速道路请求示例:

waypoints = [arrays of coordinates]
snap_to_roads_url = "https://roads.googleapis.com/v1/snapToRoads?path=#{waypoints.join('|')}&interpolate=true&key=API_KEY"
snappedPoints = HTTParty.get(URI.encode(snap_to_roads_url))

# Store snapped polyline returned by the snap-to-road service
snappedPoints['snappedPoints'].each do |snap|
  snappedCoordinates << "#{snap['location']['latitude']},#{snap['location']['longitude']}"
  placeIdArray << snap['placeId']
end

Static Map request: 静态地图请求:

https://maps-api-ssl.google.com/maps/api/staticmap?&size=650x550&sensor=false&zoom=16&markers=size:small&maptype=roadmap&path=weight:3%7Ccolor:red%7C54.57424022982199,-1.2351261042386523|54.57423420000001,-1.2351275000000002|54.57423420000001,-1.2351275000000002 ...many more

And also any idea how can I include markers with the above request, given I have origin and destination coordinates . 以及在我有起点和终点坐标的情况下,如何在上述请求中包括标记的任何想法。

Please help! 请帮忙!

I would suggest, for your use-case, that you instead use the Directions API Web Service. 对于您的用例,我建议您改用Directions API网络服务。 You can also put multiple waypoints (up to 23 per request) and its return will generate an encoded polyline of your path. 您还可以放置多个航路点(每个请求最多23个),其返回将生成路径的编码折线。 It will also provide travel distance, travel time, etc. 它还将提供旅行距离,旅行时间等。

Learn more about it here: https://developers.google.com/maps/documentation/directions/start 在此处了解更多信息: https : //developers.google.com/maps/documentation/directions/start

Snap To Roads need your coordinates to be very near each other to work properly and your static map url might get too lengthy after a few points. 贴紧道路需要您的坐标彼此之间非常接近才能正常工作,并且静态地图网址在经过几点后可能会变得太长。 Some browsers may reach its url limit, and Static Maps has an 8192 character limit 某些浏览器可能达到其网址限制,并且静态地图的限制8192个字符

Here's a sample request with the Directions Web Service (just include your own API key): https://maps.googleapis.com/maps/api/directions/json?origin=-37.59431782470163%2C144.91110764394534&destination=place_id%3AEipTeWRuZXkgUm9hZCwgU29tZXJ0b24sIFZpY3RvcmlhLCBBdXN0cmFsaWE&mode=driving&waypoints=place_id%3AEjJNaXRmb3JkIENyZXNjZW50LCBDcmFpZ2llYnVybiwgVmljdG9yaWEsIEF1c3RyYWxpYQ%7Cplace_id%3AEi5FZGluZ3RvbiBXYXksIENyYWlnaWVidXJuLCBWaWN0b3JpYSwgQXVzdHJhbGlh&key=YOUR_API_KEY 这是一个使用Directions Web服务的示例请求(仅包含您自己的API密钥): https://maps.googleapis.com/maps/api/directions/json?origin=-37.59431782470163%2C144.91110764394534&destination=place_id%3AEipTeWRuZXkgUm9hZCwgU29tZXJ0b24sIFZpY3RvcmlhLCBBdXN0cmFsaWE&mode=driving&waypoints=place_id%3AEjJNaXRmb3JkIENyZXNjZW50LCBDcmFpZ2llYnVybiwgVmljdG9yaWEsIEF1c3RyYWxpYQ%7Cplace_id%3AEi5FZGluZ3RvbiBXYXksIENyYWlnaWVidXJuLCBWaWN0b3JpYSwgQXVzdHJhbGlh&key=YOUR_API_KEY : https://maps.googleapis.com/maps/api/directions/json?origin=-37.59431782470163%2C144.91110764394534&destination=place_id%3AEipTeWRuZXkgUm9hZCwgU29tZXJ0b24sIFZpY3RvcmlhLCBBdXN0cmFsaWE&mode=driving&waypoints=place_id%3AEjJNaXRmb3JkIENyZXNjZW50LCBDcmFpZ2llYnVybiwgVmljdG9yaWEsIEF1c3RyYWxpYQ%7Cplace_id%3AEi5FZGluZ3RvbiBXYXksIENyYWlnaWVidXJuLCBWaWN0b3JpYSwgQXVzdHJhbGlh&key=YOUR_API_KEY

For a more visual representation you go to this (Include your API key to see JSON results) link 要获得更直观的表示,请转到此(包括您的API密钥以查看JSON结果) 链接

Then in its result, you can get an encoded polyline at its property: routes.overview_polyline . 然后,在其结果中,可以在其属性处得到一条编码的折线: routes.overview_polyline You can use that as your encoded polyline path like so: &path=geodesic:true%7Ccolor:0x0057E7ff%7Cenc:ENCODED_POLYLINE_HERE 您可以将其用作编码的折线路径,如下所示: &path=geodesic:true%7Ccolor:0x0057E7ff%7Cenc:ENCODED_POLYLINE_HERE

Your markers' coordinates can be retrieved by getting each leg of the route. 您可以通过获取路线的每一条路线来检索标记的坐标。 Start with routes[0].legs[0].start_location and end_location Use those coordinates for your markers. routes[0].legs[0].start_locationend_location将这些坐标用作标记。

For your total driving distance, every leg has a distance property with numerical values. 对于您的总行驶距离,每条leg都有一个带有数值的distance属性。 Just add all the legs for your total distance and even time. 只需增加所有legs的总距离和时间即可。

Your static map request will be similar to the attached code below. 您的静态地图请求将与以下所附代码相似。

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <img src="https://maps.googleapis.com/maps/api/staticmap?size=800x800 &maptype=roadmap &key=AIzaSyCD0rWgXRVBa7PgaTWXVp_gU51pgHGviYY&markers=color:blue%7Clabel:A%7C-37.5943328,144.9110993&markers=color:blue%7Clabel:B%7C-37.60746779999999,144.9393146&markers=color:blue%7Clabel:C%7C-37.6118511,144.9329711&markers=color:blue%7Clabel:D%7C-37.6302591,144.9490315&path=geodesic:true%7Ccolor:0x0057E7ff%7Cenc:psmdFk}msZeApE~APxAJvK~@pCLdA@dD?fACbESlEYbAIlB[`Dg@bBYtJeCbEcAfDo@q@_D]cBM_AEg@Eu@@sAJyAVuArAoFRqAJ_A?o@CACECG@OFKBABCJu@NyBEeAq@kEaAsGI}@EwA^uG@[AACGAEBMBC@A?{@@M?o@QuAa@sAq@yASu@Ko@I}A?u@V}DLyBAQA[?SAAAG?OFKLAHFbAL|JdALBRHZ@P?H@@ABEBAFg@FKj@gJT}CTqAZeAVs@bC}EVk@Lc@NaAFaAAy@IkAUq@A?EAGK?O?AOg@U_@o@q@g@[e@Q[Ie@G_CS_ACgAH_D^KFSF?@A@A@C@EA?AYAGC[?u@Es@Ok@Sm@_@k@e@eBmBUY`CkClE}Eh@[\\Gd@Ce@B]FWLQLmE|EaCjCTXdBlBj@d@l@^j@Rr@Nt@DZ?FEJAJA@A?ABAF@RAL@dEe@bAAfAFfBN^Hb@Lf@Vf@`@n@v@`@^F@BBDLALDb@AXHjA@x@G`AGh@VHl@Z`@\\x@fAy@gAa@]m@[WIFi@FaAAy@IkAUq@A?EAGK?O?AOg@U_@o@q@g@[e@Q[Ie@G_CS_ACgAH_D^KFSF?@A@A@G@AA?AYAGC[?u@Es@Ok@Sm@_@k@e@eBmBe@q@a@s@w@gBg@cBEEKe@A?CC?A?I?AG{@KcAGoACwBGk@Ok@Qi@Ye@YWSSUKA@CAGE?Ec@I_@Ka@QmKkD_@Ec@Mc@]ICCCQGe@EKAiAHUDa@NOLGRU`BKb@Gf@?h@DH@LELGFIBKAIGAA?Ai@GOKi@O}@Y_@E]Mo@c@e@s@Se@Mi@E[?S?GRCzBW~A]vBu@tFqCtCyAzAi@lA[rC]hCOnIa@`[uApQ}AvL_A~D]nBMhBQdBKdSkBnUsBfb@aDzD]Bv@q@H"/> </body> </html> 

If you want things to be interactive though (similar to the Directions webapp I linked) then you'll need to use the [Google Maps Javascript API Directions Service] ( https://developers.google.com/maps/documentation/javascript/directions ). 如果您希望事情是互动的(类似于我链接的Directions网络应用程序),则需要使用[Google Maps Javascript API Directions Service]( https://developers.google.com/maps/documentation/javascript/指示 )。

Hope these help! 希望这些帮助!

Here's some more about encoded polylines and markers with static map: https://developers.google.com/maps/documentation/static-maps/intro#Markers https://developers.google.com/maps/documentation/static-maps/intro#Paths 以下是有关带有静态地图的编码折线和标记的更多信息: https : //developers.google.com/maps/documentation/static-maps/intro#Markers https://developers.google.com/maps/documentation/static-maps /介绍#路径

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

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