开发者

Using GPS Map How to calculate the distance between the Two points [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

GPS coordinates in degrees to calculate distances

using GPS I am capure the string string how to separate latitude and longitude in string <+37.33168900, -122.03073100> +/- 100.00m (speed -1.00 mp / course -1.00) @ 2011-08-26 12:56:57 +0530

when i am calling location manager this is method is called

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation    *)newLocation fromLocation:(CLLocation *)oldLocation
{
   NSLog@("%@",newLocation) //<+37.33168900, -122.03073100> +/- 100.00m (speed -1.00 mp / course -1.00) @ 2011-08-26 12:56:57 +0530

   NSLog@("%@",oldLocation) //null
}

My problem is how to save my old location and how to using newlocation and ol开发者_如何学Pythondlocation how to calculate the distance i searched for google but i can't find out please help me. i am very struggle for how to save oldLocation.


You can use - (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

In order to save the two locations, you should use a property.


If you want to get latitude and longitude from string <+37.33168900, -122.03073100> +/- 100.00m (speed -1.00 mp / course -1.00) @ 2011-08-26 12:56:57 +0530 you could get it with regex by finding in first < > and separate by comma. How to do that is your work if you want to do it this way, but SDK provide you with more convenient read Location Awareness Programming Guide for more information.


Whenever you start watching your location, the first call of this method might have an oldLocation which is null. Just because your iPhone doesnt know where it was before.

if you start and stop updating your location several times in your app and you still want to know what the last location was just hold a static object which represents your oldLocation and update it after receiving the delegation call.

static CLLocation * myLastLocation;

- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation*) newLocation fromLocation:(CLLocation *)oldLocation
{
     NSLog@("Working with the new location: %@",newLocation); 

     // Save it as the old location
     myLastLocation = newLocation;

     // Your problem still exists that the first call will have myLastLocation == nil!
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜