在Xcode中打开本地谷歌地图 [英] Opening native google maps in Xcode

查看:97
本文介绍了在Xcode中打开本地谷歌地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我希望用户能够按下一个按钮,该按钮可以向该地点转弯。我知道我不允许在应用中执行此操作,但我想知道是否有人可以告诉我是否可以创建指向本地Google地图应用的链接,该应用会从用户当前位置输入路线指向位置。 / p>

如果有可能,你还可以告诉我怎么做?



任何帮助将不胜感激。



谢谢

解决方案

当然,这是可能的 - 你必须告诉系统打开谷歌地图链接,设置开始和结束地址的参数。



您可能想尝试使用以下Google地图网址:


http:/ /maps.google.com/maps?saddr=x,y&daddr=x,y


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



以下是我写的一些代码,可以将用户从当前位置移动到特定位置(硬编码在我的例子中)。

这是核心位置委托方法,一旦它们的位置建立,它就被调用。

   - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation :( CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if(newLocation.horizo​​ntalAccuracy == oldLocation.horizo​​ntalAccuracy){
[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];




$ b $ p
$ b

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

  self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];

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



希望有帮助


Hi I have an iPhone application using maps and locations.

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

解决方案

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

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.

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:

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.

Hope that helps

这篇关于在Xcode中打开本地谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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