【问题标题】:iOS Google Maps QuestioniOS 谷歌地图问题
【发布时间】:2010-12-13 19:51:04
【问题描述】:

我已查看有关将用户发送到 Google 地图应用程序的 Apple 文档 (http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html),我有一个问题, "如何让它自动显示从用户当前位置到预定纬度/经度位置的路线?

Apple Docs 说源和目标分别使用“saddr=”和“daddr=”,但没有说明如何获取用户的当前位置。是否有某种关键字(例如“saddr=Current”或“saddr=Here”)可以获取用户的位置?

显然,这是行不通的:

[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=Here&daddr=Chicago"]];

..如果我试图将用户发送到芝加哥。有什么想法吗?

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    来源: How to invoke iPhone Maps for Directions with Current Location as Start Address

    您需要使用核心位置来获取当前位置,但使用该纬度/经度对,您可以让地图将您从那里路由到街道地址。像这样:

    CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
       NSString* address = @"123 Main St., New York, NY, 10001";
       NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@",
                        currentLocation.latitude, currentLocation.longitude,
                        [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
       [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
    

    【讨论】:

    • 我在 CLLocation 行收到“Invalid Initializer”错误。
    • 看起来你需要添加一个 -(CLLocationCoordinate2D)getCurrentLocation;方法到你在其中调用这段代码的类,它返回一个 CLLocationCoordinate2D,用于 url 实例化。
    【解决方案2】:

    我认为您不能直接执行此操作。但是您可以使用saddr=lat,long 格式将坐标放入参数中(例如addr=53.453209,-0.32930)。因此,如果您在发送到 Google 地图之前计算出用户在您的应用中的当前位置,您将获得该功能的近似值。

    【讨论】:

      【解决方案3】:

      您可以在 url 的 saddr 部分中使用 Current+Location。这适用于 iOS,但我找不到适用于 Android 的。

      【讨论】:

        【解决方案4】:

        只需使用 daddr 参数集调用 Google Maps URL,Google Maps 就会在 From 字段中自动插入用户的当前位置。

        http://maps.google.de/maps?daddr=SOMEADDRESS

        【讨论】:

        • 抱歉,这只适用于模拟器(例如,当谷歌地图在 Safari Mobile 而不是原生应用中启动时)
        【解决方案5】:

        我相信saddr=Current%20Location&daddr=...是你想要的。

        【讨论】:

        • "Current Location" 不起作用,因为它破坏了 URL。"Current"、"CurrentLocation" 和 "Current_Location" 都不起作用。
        • 没有注意到你没有转义你的字符串。我现在将使用转义的空格字符进行编辑。
        • 很遗憾,如果手机设置为非英语语言,&saddr=Current%20Location 将不起作用。
        • @chris 啊,好点,我没有用另一种语言测试它。我想有本地语言版本可以工作,虽然我什么都没找到。
        • iPad2,iOS 4.3:使用此设置,地图应用程序会将当前位置与其他位置相匹配(在我的情况下,“当前,Marilao,菲律宾”离我所在的位置不远)。
        【解决方案6】:

        您总是可以在 iOS 中使用反向地理编码器来获取当前位置地址(假设用户允许您获取他们的位置)并在 URL 中使用它,这里 MKReverseGeocoder 是对用于反向地理编码的类的引用.

        -丹尼尔

        【讨论】:

          【解决方案7】:
          NSString* directionsURL = [NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",self.mapView.userLocation.coordinate.latitude, self.mapView.userLocation.coordinate.longitude, mapPoint.coordinate.latitude, mapPoint.coordinate.longitude];
          if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
              [[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL] options:@{} completionHandler:^(BOOL success) {}];
          } else {
              [[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL]];
          }
          

          【讨论】:

            【解决方案8】:

            您可以在 url 的源部分使用 CurrentLocation 坐标,在目标部分使用destinationLocation 坐标。这适用于 iOS...

            https://maps.googleapis.com/maps/api/directions/json?origin=(originCoordinate)&destination=(destinationCoordinate)&mode=driving&key=YOUR-KEY

            【讨论】: