I have a mapview with many annotations on it. When the user clicks on an annotation, it takes them to a details page with information about an artpiece there and a button to "Get directions to this location". So here is what I'm trying to decide. My mapview that I was just on had the current user location, which is what I need for a starting point in the URL to send to the maps app. Would it be best to pass the most recent userLocation coordinate from the mapview to the next page so that I can just use that when the user wants directions, or should I start a new CLLocationManager on the next page just to get the users location. Also, I tried the latter and was unable to get the user's location from a manager. All I did was declare the manager in the interface:
CLLocationManager *manager;
"property" it:
@property (nonatomic, retain) CLLocationManager *manager;
"synthesize" it:
@synthesize manager;
do this in the viewDidLoad:
[manager startUpdatingLocation];
and then do this in a "navigate" IBAction method:
-(IBAction)navigate; {
CLLocationCoordinate2D start = manager.location.coordinate;
CLLocationCoordinate2D end = {[selectedArtPiece.latitude doubleValue], [selectedArtPiece.longitude doubleValue]};
[[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString: [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, end.latitude, end.longitude ]]];
}
"end" ends up being 0,0 so I must be missing something.
So my questions are:
1. What am I doing wrong with the CLLocationManager.
2. Assuming I even get that to work, would you recommend doing that to get the current location, or should I send a slightly outdated(maybe) u开发者_如何学JAVAserlocation from the previous mapview to use for the directions? Is it worth the battery and time to start a CLLocationManager just for the sake of getting a coordinate for directions?
Thanks for any feedback!
To use the CLLocationManager
, you'll need alloc+init it, set its delegate, then call startUpdatingLocation
, and read the current location (if found) in the delegate method locationManager:didUpdateToLocation:fromLocation:
.
However, in your case, I wouldn't bother trying to get the current location again in real time. The time between the user tapping on the annotation and the detail page coming up won't be long to create a significant difference. Instead, I'd add a startingLocation property to the detail page and set it to the map view userLocation before presenting the detail page.
This way, the user doesn't have to (possibly) wait until a new, more accurate current location is found and it actually shows the directions from what the user saw as their current location before they tapped the annotation.
精彩评论