如何从我的应用中打开缩短的Google地图网址?

[英]How to open a shortened Google Maps URL from my app?


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.

我正在尝试打开Goog​​le地图应用,以便在地图中显示某些地方,每次用户点击某个按钮时,我的应用都会打开谷歌地图,问题是我唯一拥有的是缩短的Google网址,例如http:// www.goo.gl / maps / XXXXX; XXXXX根据所选位置而变化的位置。

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.

有没有办法用Google Maps SDK打开此网址?我已阅读此页面上的信息https://developers.google.com/maps/documentation/ios/,但未提供有关此案例的信息。

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!

1 个解决方案

#1


6  

Shortened URL probably not supported in Google Maps iOS sdk url scheme.

Google地图iOS sdk网址方案可能不支持缩短的网址。

You can use Google URL Shortener API to convert your shortened url back to a long url.

您可以使用Google URL Shortener API将缩短的网址转换回长网址。

Sample request:
GET https://www.googleapis.com/urlshortener/v1/url?shortUrl=https%3A%2F%2Fgoo.gl%2Fmaps%2FviRnZ&key={YOUR_API_KEY}

样例请求: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.

您可以使用此链接中的缩短网址尝试API请求。

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

从API响应中,您可以获得一个长网址,如下所示:https://www.google.com/maps/@37.4249154,-122.0722049,13z然后您可以将纬度和经度解析为变量,并将其用于您的iOS sdk网址方案的中心参数,例如37.4249154,-122.0722049是该位置的中心,13是缩放,那么您的网址方案将是@“comgooglemaps://?center = 37.4249154,-122.0722049&zoom = 13

This documentation will give tell you details about Google Maps iOS sdk url scheme.

本文档将告诉您有关Google Maps iOS sdk url方案的详细信息。

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:

请求长网址并在Google地图中打开的完整示例代码:

  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:&regexError];
                    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];
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/04/28/aba6dbbe65b81897b59b6a70d85d1900.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告