ios - 如何从我的应用中打开缩短的 Google map URL?

标签 ios objective-c google-maps

我试图打开谷歌地图应用程序以显示 map 中的一些地方,每次用户点击某个按钮时,我的想法是我的应用程序将打开谷歌地图,问题是我唯一拥有的是缩短的 Google 网址,例如 http://www.goo.gl/maps/XXXXX; XXXXX 根据他们选择的位置而变化。

当用户单击按钮时,我将检查他们是否安装了 Google map ,如果他们没有安装,我将打开 Safari,这很好,但我不知道如何为 Google map 应用程序执行此操作。

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

这是我的代码的一部分:

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
     //Open Google Maps App
 }else{
     [[UIApplication sharedApplication] openURL:selectedPlace.googleMapsLocation];
 }

谢谢!

最佳答案

Google Maps iOS sdk url 方案可能不支持缩短的 URL。

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

sample 要求:GET https://www.googleapis.com/urlshortener/v1/url?shortUrl=https%3A%2F%2Fgoo.gl%2Fmaps%2FviRnZ&key={YOUR_API_KEY}
您可以尝试使用来自 this link 的缩短 URL 的 API 请求.

从 API 响应中,您可以获得一个长 URL,如下所示:https://www.google.com/maps/@37.4249154,-122.0722049,13z然后您可以将纬度和经度解析为变量,并将它们用于center您的 iOS sdk url 方案的参数,例如 37.4249154,-122.0722049是位置的中心,13是缩放,那么您的 url 方案将是 @"comgooglemaps://?center=37.4249154,-122.0722049&zoom=13
This documentation将告诉您有关 Google Maps iOS sdk url 方案的详细信息。

示例代码:

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://");
}

请求长 url 并在谷歌地图中打开的完整示例代码:
  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];

关于ios - 如何从我的应用中打开缩短的 Google map URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29926521/

相关文章:

ios - 使用 AVRoutePickerView 链接错误

google-maps - 每 100 米后获取沿途点的纬度/经度

javascript - 谷歌地图标记网址

javascript - 从 QWebView *Google Maps API、PyQT* 运行 JavaScript 函数

ios - 如果您不是开发人员计划的一部分,请下载 Xcode/ios

ios - 具有模拟器支持的 Core Image 着色器语言转换为 Metal

ios - ios中输入框的占位符旁边有红色星号吗?

objective-c - setShowsTouchWhenHighLighted 不起作用

iOS:将图像保存在 App Group 中以便在 Apple Watch 上使用

ios - 具有半透明效果的导航 Controller