Reputation: 492
I'm trying to open the Google Maps app in order to show some places in a map, each time the user clicks a certain button the idea is that my app will open Google Maps, the problem is that the only thing I have is a shortened Google URL, for example http:// www.goo.gl /maps/XXXXX; where the XXXXX changes depending on the location they chose.
When the user clicks the button I will check if they have Google Maps installed, in case they don't I'll open Safari, this works just fine, but I don't know how to do it for the Google Maps App.
Is there a way to open this URL with Google Maps SDK? I've read the information on this page https://developers.google.com/maps/documentation/ios/, but no information is provided about this case.
This is the part of my code:
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
//Open Google Maps App
}else{
[[UIApplication sharedApplication] openURL:selectedPlace.googleMapsLocation];
}
Thank you!
Upvotes: 1
Views: 2504
Reputation: 6921
Shortened URL probably not supported in Google Maps iOS sdk url scheme.
You can use Google URL Shortener API to convert your shortened url back to a long url.
Sample request:
GET https://www.googleapis.com/urlshortener/v1/url?shortUrl=https%3A%2F%2Fgoo.gl%2Fmaps%2FviRnZ&key={YOUR_API_KEY}
You can try the API request with your shortened url from this link.
From the API response, you can get a long url, something like this: https://www.google.com/maps/@37.4249154,-122.0722049,13z
Then you can parse the latitude and longitude to variables, and use them for the center
parameter of your iOS sdk url scheme, for example 37.4249154,-122.0722049
is the center of the location, 13
is the zoom, then the your url scheme will be @"comgooglemaps://?center=37.4249154,-122.0722049&zoom=13
This documentation will give tell you details about Google Maps iOS sdk url scheme.
Sample code:
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]]) {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:@"comgooglemaps://?center=37.4249154,-122.0722049&zoom=13&views=traffic"]];
} else {
NSLog(@"Can't use comgooglemaps://");
}
Full sample code to request long url and open in Google map:
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:@"https://www.googleapis.com/urlshortener/v1/url?shortUrl=https://goo.gl/maps/viRnZ&key=YOU_API_KEY"] completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
NSLog(@"dataTaskWithRequest HTTP status code: %ld", (long)statusCode);
return;
}
}
NSError *jsonParseError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParseError];
if (!jsonParseError) {
NSLog(@"%@", json);
NSString *longUrl = [json objectForKey:@"longUrl"];
NSString *pattern = @".*?@([0-9.\\-]*),([0-9.\\-]*),([0-9.\\-]*).*";
NSError *regexError = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:®exError];
if (!regexError) {
NSArray* matches = [regex matchesInString:longUrl options:0 range:NSMakeRange(0, [longUrl length])];
NSString *latitude = [longUrl substringWithRange:[[matches objectAtIndex:0] rangeAtIndex:1]];
NSString *longitude = [longUrl substringWithRange:[[matches objectAtIndex:0] rangeAtIndex:2]];
NSString *zoom = [longUrl substringWithRange:[[matches objectAtIndex:0] rangeAtIndex:3]];
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]]) {
NSString *openURL = [NSString stringWithFormat:@"comgooglemaps://?center=%@,%@&zoom=%@&views=traffic", latitude, longitude, zoom];
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:openURL]];
} else {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:longUrl]];
}
} else {
NSLog(@"REGEX error: %@", regexError);
}
} else {
NSLog(@"JSON parse error: %@", jsonParseError);
}
} else {
NSLog(@"API request error: %@", error);
}
}] resume];
Upvotes: 6