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

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

问题描述

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

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

第1行中的[self getCurrentLocation]此处使用CLLocationManager确定当前位置并返回值.

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

注意:我尚未在 LINE1 中实现代码.我只是打算那样做.

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

我的问题是:

  1. 在调用地图"应用时,这是一种计算当前位置的好习惯吗?
  2. [self getCurrentLocation]将在调用openURL之前返回当前位置吗?
  3. 在打开地图"应用之前,我是否必须确定当前位置?
  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.

您可以传递Current%%20Location而不是传递纬度/经度对,而Maps会确定用户的当前位置本身.

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

%20是URL编码的空格字符,多余的%转义了实际的%,因此不会被解释为格式替换.

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

感谢 @Carlos P 在原始答案中指出我的逃生角色失误.

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

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

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