从代码打开地图应用程序 - 在哪里/如何找到“当前位置”? [英] Open Maps app from Code - Where/How to find the "Current Location"?

查看:171
本文介绍了从代码打开地图应用程序 - 在哪里/如何找到“当前位置”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开地图应用程序以显示从用户的当前位置到目标坐标的方向,从我的代码显示。我正在使用以下代码来打开地图应用程序。我在按下按钮时调用此代码。 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]];
}

这里 [self getCurrentLocation] >第1行中的使用 CLLocationManager 来确定当前位置并返回该值。

注意:我还没有在 LINE1 中实施代码。我刚刚计划这样做。



我的问题是:


  1. 在地图应用程序被调用的时候,计算当前位置是否是一种好的做法?

  2. 请问 [self getCurrentLocation ]在 openURL 被调用之前返回当前位置?

  3. 在打开地图应用之前,我必须确定当前位置吗?

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

解决方案

您无需亲自确定用户的当前位置,地图应用会照顾它。 / p>

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

%20 是一个网址编码的空格字符,而额外的转义实际的,所以它不会被解释为格式替换。






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


I am opening Maps app to show directions from user's Current Location to a destination coordinate, from my code. I am using the following code to open the Maps app. I am calling this code when a button is pressed. getCurrentLocation is a method that returns the recently updated location.

- (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]];
}

Here [self getCurrentLocation] in LINE 1 uses CLLocationManager to determine the Current Location and returns the value.

Note: I have not yet implemented the code in LINE1. I've just planned to do it that way.

My questions are:

  1. Is this good practice to calculate the Current Location, at the time the Maps app is called?
  2. Will [self getCurrentLocation] return the Current Location before openURL gets called?
  3. Do I have to determine the Current Location well before opening the Maps app?

I am little bit confused about these things. Kindly guide me. Thanks.

解决方案

You don't have to determine the user's current location yourself, the Maps app will take care of it.

Instead of passing a latitude/longitude pair you can pass Current%%20Location and Maps will determine the user's current location itself.

%20 is a url-encoded space character, and the extra % escapes the actual % so it won't be interpreted as a format substitution.


Thanks to @Carlos P for pointing out my escape character blunder in the original answer.

这篇关于从代码打开地图应用程序 - 在哪里/如何找到“当前位置”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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