计算iphone中两点之间的距离
我正在创建一个应用程序,要求用户输入两个地方(邮政编码)。我的应用程序将计算这两点之间的行驶距离并输出结果。用户可以选择添加航路点。 我想我必须使用谷歌地图 API 并获取包含结果的 xml 文件,然后解析该 xml 文件。任何人都可以帮助我,因为我不知道该怎么做。 感谢所有帮助。
例子... 起点:BR1 1LR
航点:BR2 0LH
航点:BR3 4AY
目的地:BR1 1LR
I am creating an application that requires the user to input two places (Postal codes). My application will calculate the driving-distance between these two points ad output the result. The user has the option of adding Way-Points.
I think I would have to use the google maps API and get an xml file with the result and then parse the xml file. Can anyone help me because i not sure how to do this.
Appreciate all help.
Example...
Start: BR1 1LR
Waypoint: BR2 0LH
Waypoint: BR3 4AY
Destination: BR1 1LR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,你可以计算距离,因为你需要有纬度和经度才能得到距离。Objective C提供了计算点之间距离的方法
这是示例......
这将为您提供以公里为单位的距离。
Yes you can calculate the distance ,for that you need to have the latitude and longitude to get the distance.Objective C provides the method to calculate the distance between points
here is the example......
This will provide you the distance in Kilometers.
我知道这真的很晚了,但我将发布我的解决方案,以防有人需要。
首先,我声明了 URL(调用 google 地图 api 的 URL)
接下来,我创建了一个包含此 URL 的字符串:
其中源、目的地是包含起点和终点的字符串。单位可以是@“英制”或@“公制”。
现在我有了 URL,我将返回一个 xml 字符串。为了解析这个,我使用了 TBXML。关于使用哪种 XML 解析器始终存在很大的争论。我使用了 TBXML 因为它很简单,并且是 最快。
检查根状态正常后,即可获取 XPath 表达式的结果: //row/element/distance/value
如果有人想要一个包含路径点的 URL,那么这里就是
但是要使用路径点,事情就可以了正如贾诺指出的,情况有些不同。
I know this is really late, but I'm posting my solution to this in case anybody needs it.
Firstly, i declared the URL (the one that calls google maps api)
Next I created a string containing this URL:
Where source, destination are strings containing the start point and end point. units can be @"imperial" or @"metric".
Now that i had the URL, i would get back an xml string. To parse this, i used TBXML. There is always a large debate about which XML Parser to use. I used TBXML as it was easy, and is the fastest.
Once checking the root status is OK, then obtain the result for the XPath expression: //row/element/distance/value
If anyone wants to have a URL to include way points, then here it is
But to use way points, things work a bit differently as Jano pointed out.
如果您想要了解道路距离,则需要询问 Google,然后解析生成的 XML ,使用 XML 解析器和 XPath 表达式
//leg/step/distance/value
,这将为您提供必须求和的所有距离。请参阅此内容了解如何解析 xml 和使用 XPath 表达式。If you want the road distance you need to ask Google and then parse the resulting XML, using a XML parser and the XPath expression
//leg/step/distance/value
, which will give you all the distance, that you will have to sum. See this about parsing xml and using XPath expressions.distanceFromLocation: 方法将为您提供任意两个 CLLocation 点之间的绝对距离,而不是行驶距离。为此,您必须像您怀疑的那样使用 Google Maps API。
The distanceFromLocation: method will give you the absolute distance between any two CLLocation points, not a driving distance. For that, you must use the Google Maps API as you suspected.