【问题标题】:Using Current Location from Location Manager in a Google Maps openURL?在 Google 地图 openURL 中使用位置管理器中的当前位置?
【发布时间】:2010-10-07 15:25:38
【问题描述】:

今天一直在研究这个问题,但我似乎无法理解如何将用户当前位置输入 Google 地图 URL 以打开填充了方向的 Google 地图应用。

我已从另一个教程中获取此代码并将其添加到我的应用程序中。我需要将下面的“开始”纬度/经度输入到我的 IBAction 中才能打开 Google 地图应用。

- (void)locationUpdate:(CLLocation *)location {
    // Lat/Lng of user (Needs to be sent to IBAction for btnDirections)
    CLLocationCoordinate2D start = { location.coordinate.latitude, location.coordinate.longitude };
}

以下是我的“获取路线”按钮的 IBAction。

- (IBAction) onButtonClick:(id) sender {
    if(sender == btnDirections) {
        CLLocationCoordinate2D destination = { 52.48306771095311, -1.8935537338256836 };            
        NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
                                         newLocation.coordinate.latitude, newLocation.coordinate.longitude, destination.latitude, destination.longitude];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];      
    }
}

我之前在另一个应用程序中做过此操作,但是该视图包含显示用户位置的地图,这使我可以轻松获取纬度/经度。

提前致谢。

【问题讨论】:

    标签: iphone


    【解决方案1】:

    只需将“当前+位置”作为参数传递给谷歌地图应用程序:

    http://maps.google.com/maps?saddr=Current+Location&daddr=Heathrow+Airport+London
    

    【讨论】:

    • 它不适用于与英语不同的设备语言,唉。
    • 或在 iPad 上,甚至是英文
    【解决方案2】:

    在我的代码中,我的 URL 格式为:

    http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f
    

    (这就是你添加的“地图”)。

    除此之外,您可以将位置请求设置为在设定的精度后停止,您设置的精度是否足以满足您的目的?

    哦,最后一点,在模拟器中获取当前位置可能不起作用。

    【讨论】:

    • 位置在我的模拟器中工作。位置更新背后的想法是,如果人们正在前往/离开目的地,那么它会打开谷歌地图,其中包含几分钟前的最新位置的路线。
    【解决方案3】:

    您需要使用户的当前位置可用于 onButtonClick: 方法。您正在为每个位置更新创建起始结构,但您永远不会在该方法之外使其可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 2013-10-27
      • 1970-01-01
      相关资源
      最近更新 更多