如何从我的应用中打开缩短的Google地图网址? [英] How to open a shortened Google Maps URL from my app?

查看:108
本文介绍了如何从我的应用中打开缩短的Google地图网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开Goog​​le地图应用以显示地图中的某些地方,每次用户点击某个按钮时,我的应用会打开Goog​​le地图,问题在于我唯一已缩短的Google网址,例如: http://www.goo.gl/maps/XXXXX ;



当用户点击按钮时,我会检查他们是否安装了Google地图,如果他们不是我的将打开Safari,这工作得很好,但我不知道如何去做Google Maps App。



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



这是我的代码的一部分:


$ b $(

  if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@comgooglemaps://]]){
//打开Goog​​le Maps App
} else {
[[UIApplication sharedApplication] openURL:selectedPlace.googleMapsLocation];
}

谢谢!

解决方案

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



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

示例请求:

GET https://www.googleapis.com/urlshortener/v1/url?shortUrl=您可以尝试API缩短后的API请求来自此链接的网址。

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



此文档会告诉您有关Google地图iOS sdk url方案的详细信息。



示例代码:

  if([[UIApplication sharedApplication] canOpenURL :
[NSURL URLWithString:@comgooglemaps://]]){
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:@comgooglemaps://?center = 37.4249154, -122.0 722049和缩放= 13&安培;视图=交通]];
} else {
NSLog(@无法使用comgooglemaps://);
}

完整示例代码,用于请求长网址并在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状态码:%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解析错误:%@,jsonParseError);
}
} else {
NSLog(@API request error:%@,error);
}
}] resume];


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've is a shortened Google URL, for example: http://www.goo.gl/maps/XXXXX; where the XXXXX changes depending on the location they are watching.

When the users 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 in 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!

解决方案

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:&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];

这篇关于如何从我的应用中打开缩短的Google地图网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆