【问题标题】:Open Maps app from Code - Where/How to find the "Current Location"?从代码打开地图应用程序 - 在哪里/如何找到“当前位置”?
【发布时间】:2011-06-03 05:19:25
【问题描述】:

我正在打开地图应用程序以显示从我的代码从用户当前位置到目的地坐标的路线。我正在使用以下代码打开地图应用程序。当按下按钮时,我正在调用此代码。 getCurrentLocation 是一个返回最近更新位置的方法。

- (void)showDirectionsToHere {

    CLLocationCoordinate2D currentLocation = [self getCurrentLocation];  // LINE 1
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", 
                                                  currentLocation.latitude,
                                                  currentLocation.longitude, 
                                                  destCoordinate.latitude, 
                                                  destCoordinate.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

这里 LINE 1 中的 [self getCurrentLocation] 使用 CLLocationManager 确定当前位置并返回值。

注意:我还没有实现LINE1中的代码。我只是打算这样做。

我的问题是:

  1. 这是在调用地图应用时计算当前位置的好习惯吗?
  2. [self getCurrentLocation] 会在调用openURL 之前返回当前位置吗?
  3. 我是否必须在打开地图应用程序之前确定当前位置?

我对这些事情有点困惑。请指导我。谢谢。

【问题讨论】:

    标签: iphone ios google-maps core-location


    【解决方案1】:

    您不必自己确定用户的当前位置,地图应用会处理它。

    您可以传递Current%%20Location 而不是传递纬度/经度对,地图将自行确定用户的当前位置。

    %20 是 url 编码的空格字符,额外的 % 转义了实际的 %,因此不会被解释为格式替换。


    感谢 @Carlos P 指出我在原始答案中的转义字符错误。

    【讨论】:

    • 谢谢伙计。当我将 Current%20Location 作为saddr 传递时。地图应用程序甚至没有打开。 [NSString stringWithFormat: @maps.google.com/maps?saddr=Current%20Location&daddr=%f,%f">, destCoordinate.latitude, destCoordinate.longitude] 是我正在使用的代码。
    • 将代码放在您的问题中或在您的评论中用反引号括起来 - 它不可读。我很肯定它有效,所以这听起来像是一个语法问题。
    • 对不起.. 我使用的代码是[NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current%20Location&daddr=%f,%f", destCoordinate.latitude, destCoordinate.longitude]
    • 谁能让它在 iPad 上运行?在 iPhone/iPod touch 上没问题,但在 iPad 上,它似乎从“Current, MO”开始。 :-o
    • @MatthewF 和其他人 - 我对这些 cmets 感到困惑,因为转义百分号的正确方法是 %% ,除非我弄错了。事实上,我已经尝试过 \% 并且它根本没有解析为百分号,它产生了一系列随机数字。所以,对于未来的读者,我可以建议:maps.google.com/maps?saddr=Current%%20Location&daddr.....
    【解决方案2】:

    仅当用户将系统语言设置为英语时,才能使用“当前位置”作为 saddr。最好的选择实际上是从核心位置获取当前位置并将其用作saddr。

    【讨论】:

    • 是吗?我没有用其他语言测试过。谢谢你的提示。我会考虑到这一点。
    【解决方案3】:

    正如 pazustep 指出的那样,“当前位置”仅适用于英语。例如,在意大利语中,正确的字符串是“Posizione attuale”。

    在 iPhone 固件中嗅探我检测到所有“当前位置”翻译,并编写了一个类,该类提供任何(当前)支持的语言所需的正确字符串。

    我的博客上有一篇关于此的帖子(包括源代码):http://www.martip.net/blog/localized-current-location-string-for-iphone-apps

    【讨论】:

    • 在语言设置为英语且货币和日期设置为德语的设备上调用 currentLocale 将返回“de”,但 Google 地图仍会期望“当前位置”。请参阅this question 了解如何获取当前语言设置。
    【解决方案4】:

    您可以为 iOS 6 使用新的 MKMapItem 类。See the Apple API docs here

    基本上,如果路由到目的地坐标 (destCoordinate),您将使用这样的东西:

        MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: destCoordinate addressDictionary: nil];
        MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place];
        destination.name = @"Name Here!";
        NSArray* items = [[NSArray alloc] initWithObjects: destination, nil];
        NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
                                     MKLaunchOptionsDirectionsModeDriving, 
                                     MKLaunchOptionsDirectionsModeKey, nil];
        [MKMapItem openMapsWithItems: items launchOptions: options];
    

    为了在同一代码中同时支持 iOS 6+ 和 iOS 6 之前的版本,我建议使用 Apple 在 MKMapItem API 文档页面上提供的类似代码:

    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
       // iOS 6 MKMapItem available
    } else {
       // use pre iOS 6 technique
    }
    

    这将假定您的 Xcode Base SDK 是 iOS 6(或最新 iOS)。

    In this other answer, I offer a robust technique for iOS 5.1 and lower

    【讨论】:

    • MKMapItem 使得在地图应用程序中显示从您当前位置到 POI 的路线变得非常简单。但是为了显示附近的那些 POI,您仍然必须使用 Core Location 来获取 sql 中纬度/经度的当前位置。 mapItemForCurrentLocation 不能使用,因为它没有地标,因此不会产生您需要的纬度/经度。
    • @SPA,嗯?你在说什么SQL?你的意思是说“URL”吗?这个 iOS 6 解决方案仍然不需要 URL。这个问题是关于寻找从当前位置到目的地的方向。无需使用mapItemForCurrentLocation
    • 不,我的意思是 SQL。如果您有地点数据库,要显示最近的地点,您需要将当前位置作为 lat/long 来制作 SQL Select 语句。所以核心位置仍然是必需的。
    • @SPA,你在回答什么问题?这个问题与 SQL 或任何其他格式的地点数据库无关。
    • 它确实说的是目的地坐标。所以这必须来自某个地方。用户可以从列表中选择它。只有在您知道当前位置的情况下才能填充该列表。
    猜你喜欢
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2012-01-19
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多