# Calculate the distance between two points in iphone

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

• React Native AirBnB Maps worldwide polygon
• Replace blue dot in GMSMapView by an arrow - Swift
• Is there a way to get directions(just routes) using google for mkmapview?
• Google Maps for iOS, swift - How to show entire polyline between markers?
• Exception on API Key of Google Maps API for iOS
• Google Maps iOS SDK and storyboards
• Waypoint: BR2 0LH

Waypoint: BR3 4AY

Destination: BR1 1LR

### 4 Solutions Collect From Internet About “Calculate the distance between two points in iphone”

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.

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.

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.

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.