计算iphone中两点之间的距离

发布于 2024-11-03 03:15:05 字数 231 浏览 1 评论 0原文

我正在创建一个应用程序,要求用户输入两个地方(邮政编码)。我的应用程序将计算这两点之间的行驶距离并输出结果。用户可以选择添加航路点。 我想我必须使用谷歌地图 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Spring初心 2024-11-10 03:15:05

是的,你可以计算距离,因为你需要有纬度和经度才能得到距离。Objective C提供了计算点之间距离的方法
这是示例......

float distance =[mUserCurrentLocation distanceFromLocation:location1]/1000;

这将为您提供以公里为单位的距离。

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

float distance =[mUserCurrentLocation distanceFromLocation:location1]/1000;

This will provide you the distance in Kilometers.

我早已燃尽 2024-11-10 03:15:05

我知道这真的很晚了,但我将发布我的解决方案,以防有人需要。

首先,我声明了 URL(调用 google 地图 api 的 URL)

#define DST_API @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=%@&destinations=%@&units=%@&sensor=false"

接下来,我创建了一个包含此 URL 的字符串:

NSString *URL = [NSString stringWithFormat:DST_API, source, destination, units];

其中源、目的地是包含起点和终点的字符串。单位可以是@“英制”或@“公制”。
现在我有了 URL,我将返回一个 xml 字符串。为了解析这个,我使用了 TBXML。关于使用哪种 XML 解析器始终存在很大的争论。我使用了 TBXML 因为它很简单,并且是 最快

TBXML *directionsParser = [[TBXML alloc] initWithURL:[NSURL URLWithString:URL]];

// Check if the Overall status is OK
TBXMLElement *root = directionsParser.rootXMLElement;
TBXMLElement *element = [TBXML childElementNamed:@"status" parentElement:root]; 
NSString *value = [TBXML textForElement:element];
if ([value caseInsensitiveCompare:@"OK"] != NSOrderedSame) {
    [directionsParser release];
    return result;
}

检查根状态正常后,即可获取 XPath 表达式的结果: //row/element/distance/value

element = [TBXML childElementNamed:@"row" parentElement:root];
element = [TBXML childElementNamed:@"element" parentElement:element];
element = [TBXML childElementNamed:@"status" parentElement:element];
value = [TBXML textForElement:element];

// Check if the Element status is OK
if ([value caseInsensitiveCompare:@"OK"] != NSOrderedSame) {
        [directionsParser release];
    return result;
}

element = element->parentElement;

element = [TBXML childElementNamed:@"distance" parentElement:element];

//Note if you want the result as text, replace 'value' with 'text'
element = [TBXML childElementNamed:@"value" parentElement:element];

NSString *result = [TBXML textForElement:element];

[directionsParser release];
//Do what ever you want with result

如果有人想要一个包含路径点的 URL,那么这里就是

#define DIR_API @"http://maps.googleapis.com/maps/api/directions/xml?origin=%@&destination=%@&waypoints=%@&units=%@&sensor=false"

但是要使用路径点,事情就可以了正如贾诺指出的,情况有些不同。

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)

#define DST_API @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=%@&destinations=%@&units=%@&sensor=false"

Next I created a string containing this URL:

NSString *URL = [NSString stringWithFormat:DST_API, source, destination, units];

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.

TBXML *directionsParser = [[TBXML alloc] initWithURL:[NSURL URLWithString:URL]];

// Check if the Overall status is OK
TBXMLElement *root = directionsParser.rootXMLElement;
TBXMLElement *element = [TBXML childElementNamed:@"status" parentElement:root]; 
NSString *value = [TBXML textForElement:element];
if ([value caseInsensitiveCompare:@"OK"] != NSOrderedSame) {
    [directionsParser release];
    return result;
}

Once checking the root status is OK, then obtain the result for the XPath expression: //row/element/distance/value

element = [TBXML childElementNamed:@"row" parentElement:root];
element = [TBXML childElementNamed:@"element" parentElement:element];
element = [TBXML childElementNamed:@"status" parentElement:element];
value = [TBXML textForElement:element];

// Check if the Element status is OK
if ([value caseInsensitiveCompare:@"OK"] != NSOrderedSame) {
        [directionsParser release];
    return result;
}

element = element->parentElement;

element = [TBXML childElementNamed:@"distance" parentElement:element];

//Note if you want the result as text, replace 'value' with 'text'
element = [TBXML childElementNamed:@"value" parentElement:element];

NSString *result = [TBXML textForElement:element];

[directionsParser release];
//Do what ever you want with result

If anyone wants to have a URL to include way points, then here it is

#define DIR_API @"http://maps.googleapis.com/maps/api/directions/xml?origin=%@&destination=%@&waypoints=%@&units=%@&sensor=false"

But to use way points, things work a bit differently as Jano pointed out.

爱你是孤单的心事 2024-11-10 03:15:05

如果您想要了解道路距离,则需要询问 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.

野味少女 2024-11-10 03:15:05

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文