在 Xcode 中打开本机谷歌地图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8282369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:33:48  来源:igfitidea点击:

Opening native google maps in Xcode

iphoneiosxcodegoogle-mapsmapkit

提问by James Kerstan

Hi I have an iPhone application using maps and locations.

嗨,我有一个使用地图和位置的 iPhone 应用程序。

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

谢谢

回答by Alex Coplan

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

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.

所以你可以看到两个参数是saddr(起始地址)和 daddr(目标地址)。您将这些设置为一对坐标,以逗号分隔。

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:

要设置这一切,您可以将位置管理器的属性添加到您的控制器,然后当您想设置它时(例如在 中viewDidLoad)像这样初始化它:

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.

调用[[UIApplication sharedApplication] openUrl:url];会将它发送到系统的 URL 处理程序,它会检测到它是一个谷歌地图链接,并在地图中打开它。

Hope that helps

希望有帮助