Using Current Location from Location Manager in a Google Maps openURL?
Been looking into this for a while today however I can't seem to get my head around how to get the users current location into a Google Maps URL to open the Google Maps app with directions populated.
I've taken this code from another tutorial and added it into my app. I need to get the "start" lat/lng from the below into my IBAction to open the Google Maps app.
- (void)locationUpdate:(CLLocation *)location {
开发者_如何学C // Lat/Lng of user (Needs to be sent to IBAction for btnDirections)
CLLocationCoordinate2D start = { location.coordinate.latitude, location.coordinate.longitude };
}
The below is my IBAction for my "Get Directions" button.
- (IBAction) onButtonClick:(id) sender {
if(sender == btnDirections) {
CLLocationCoordinate2D destination = { 52.48306771095311, -1.8935537338256836 };
NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
newLocation.coordinate.latitude, newLocation.coordinate.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
}
}
I have done this previously in another app, however that view included a map showing the user location which allowed me to get the lat/lng easily.
Thanks in advance.
Simply pass "Current+Location" as a parameter to the google maps application:
http://maps.google.com/maps?saddr=Current+Location&daddr=Heathrow+Airport+London
In my code, I have an URL of the format :
http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f
(So that's 'maps' added from yours).
Other than that, you can setup your location request to stop after a set precision, did you setup precise enough for your purpose?
Oh, last note, getting current location might not work in simulator.
You need to make the user's current location available to the onButtonClick: method. You are creating the start structure on each location update, but you are never making it available outside of that method.
精彩评论