按距离从方向服务检索目的地坐标
我需要使用 google 地图 api 方向服务检索目的地的坐标。我已经有了起点坐标,但是我希望通过指定距离(以公里为单位)来检索坐标,而不是在坐标中指定终点。
所以我想我的问题如下:是否可以通过使用方向服务或任何替代方法指定距离(以公里为单位)来检索目的地经纬度坐标(基于/计算道路的距离而不是方向/直线)方式?
我有一个图像插图,但不幸的是我无法附加这个问题,因为我没有足够的声誉。如果我的问题有任何不清楚之处,或者您希望查看插图,请与我联系,我会将其发送出去。
I need to retrieve a destination's coordinates using the google maps api directions service. I already have the starting point coordinates, however instead of specifying an ending point in coordinates, I wish to retrieve the coordinates by specifying a distance (in km).
So I guess my question is the following: is it possible to retrieve the destination latlong coordinates (based/calculated on the road's distance and not directional/straight line) by specifying a distance (amount in km) with the directions service or perhaps any alternative way?
I have an image illustration, however unfortunately am unable to attach to this question as I do not have enough reputation. If my question is unclear in any way, or you wish to see the illustration then please contact me and I'll send it off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您不能按照请求来执行此操作参数表示起始点和目的地参数是必需的。
I don't think you can do this as the request parameters say that origin and destination parameters are required.
我相信它会对某人有所帮助。
Google 地图库:
I beliave it will help someone.
There is a method to get coordinates in the google maps library:
我相信你是对的。 api 中似乎没有任何当前方法可以让您执行以下操作。
相反,我循环遍历从方向服务调用返回的坐标,并使用函数来计算坐标之间的距离。然而,即使这样也不够准确,因为返回的坐标似乎也是聚合的,并且在计算聚合时每个坐标之间的距离时不会返回准确的值/距离,因此沿道路不需要每个坐标。
为了解决上述问题,我最终添加了一个单击事件,并自己绘制了沿路的坐标,然后将它们存储在本地 json 文件中,我缓存并使用 xmlhttprequest 调用该文件。
幸运的是,对于我的情况,我只需要计算点 A 和点 A 之间的距离。 B 在一条单独的道路上,所以当您使用多条或通用道路/位置时,我的替代方案将不起作用。鉴于您乐于接受聚合数据和不准确的计算,您可以改用所描述的第一种方法。
以下是用于计算坐标之间的距离以及最终计算找到点和坐标的函数。最后两点之间的坐标。请注意此代码依赖并使用 jQuery 方法。
1.计算两个坐标之间的距离(以米为单位)
2.获取最终 AB 坐标的坐标(基于剩余百分比)。 “matrix”变量是一个 json 坐标数组。
3.将数字度数转换为弧度
希望以下内容可以帮助任何遇到相同或类似问题的人。我没有调查这个,因为我没有必要,但是,也许如果你正在处理谷歌的付费服务,他们不会聚合调用返回的数据?
I believe you are correct. There doesn't seem to be any current method in the api which would allow you to do the following.
Instead I looped through the coordinates returned from the directions service call, and used a function to calculate the distance between coordinates. However even this was not accurate enough as the coordinates returned also seemed to be aggregated and doesn't return an accurate value/distance when calculating the distances between each coordinate as they are aggregated and therefore each coordinate is not necessary along the road.
To work around the above issue, I ended up adding a click event, and plotted the coordinates along the road myself and then stored them in a local json file which I cache and call using an xmlhttprequest.
Fortunately, for my situation I only need to calculate the distance between point A & B on one individual road, so my alternative won't work in cases when you're using multiple or generic roads/locations. You could instead use the first method described, given that you're happy to live with the aggregated data and an in-accurate calculation.
Below are the functions used to calculate the distances between coordinates and then also the final calculation to find the point & coordinates between the final two points. Please note this code relies on and uses jQuery methods.
1. Calculate distance (in meters) between two coordinates
2. Fetch coordinate of final A-B coordinate (based on percentage remaining). The 'matrix' variable is a json array of coordinates.
3. Convert numeric degrees to radians
Hope the following helps any one with the same or simular problem. I haven't investigated this as I've had no need to, but, perhaps if you're dealing with google's paid services, they don't aggregate the data returned by the call?