Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

iphone - Opening native google maps in Xcode

Hi I have an iPhone application using maps and locations.

I would like the user to be able to press a button that gives turn by turn directions to that location. I understand that i am not allowed to do this in app, but i was wondering if anyone could tell me if it is possible to make a link to the native google maps application that will enter directions to the location from the users current location.

If it is possible could you also tell me how?

any help would be appreciated.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sure, it's possible - you've just got to tell the system to open a google maps link, with parameters for the start and end address set.

You might want to try using the following google maps URL:

http://maps.google.com/maps?saddr=x,y&daddr=x,y

So you can see the two parameters are saddr (start address) and daddr (destination address). You set these to a pair of coordinates, separated by a comma.

Here is a bit of code I wrote that will take the user from their current location to a specific location (hardcoded in my case).

This is the core location delegate method which is called once their location has been established.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if (newLocation.horizontalAccuracy == oldLocation.horizontalAccuracy) {
        [self.locationManager stopUpdatingLocation];
        CLLocationCoordinate2D coords = newLocation.coordinate;
        NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%g,%g&daddr=50.967222,-2.153611", coords.latitude, coords.longitude];
        NSURL *url = [NSURL URLWithString:stringURL];
        [[UIApplication sharedApplication] openURL:url];
    }
}

To set this all up, you could add a property of a location manager to your controller, and then when you want to set it up (say in viewDidLoad) intialise it like this:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];

Calling [[UIApplication sharedApplication] openUrl:url]; will send it to the system's URL handler, which will detect it's a google maps link, and open it up in maps.

Hope that helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...