Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

iphone - Open Maps app from Code - Where/How to find the "Current Location"?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...