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

后端 未结 16 1629
一整个雨季
一整个雨季 2020-12-02 03:53

I know it\'s possible to start the iPhone maps application by calling openURL on a google maps URL with parameters saddr and daddr wit

相关标签:
16条回答
  • 2020-12-02 04:44

    I recommend checking out CMMapLauncher, a mini-library that I built to launch Apple, Google, and other iOS mapping apps with a specific mapping request. With CMMapLauncher, the code to get the directions in your question would be:

    [CMMapLauncher launchMapApp:CMMapAppAppleMaps
              forDirectionsFrom:[CMMapPoint mapPointWithName:@"Origin"
                                                  coordinate:myLatLong]
                             to:[CMMapPoint mapPointWithName:@"Destination"
                                                  coordinate:latlong]];
    

    As you can see, it also encapsulates the version checking required between iOS 6 & others.

    0 讨论(0)
  • 2020-12-02 04:45

    The real solution can be found here. Z5 Concepts iOS Development Code Snippet

    It just requires a little bit of encoding.

    - (IBAction)directions1_click:(id)sender
    {
        NSString* address = @"118 Your Address., City, State, ZIPCODE";
        NSString* currentLocation = @"Current Location";
        NSString* url = [NSStringstringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        UIApplication *app = [UIApplicationsharedApplication];
        [app openURL: [NSURL URLWithString: url]];
    }
    
    0 讨论(0)
  • 2020-12-02 04:46

    If you don't want to ask for location permissions and don't have the lat and lng, use the following.

    NSString *destinationAddress = @"Amsterdam";
    
    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
    
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) {
            if([placemarks count] > 0) {
    
                MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];
    
                MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark];
    
                MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];
    
    
                NSArray *mapItems = @[mapItem, mapItem2];
    
                NSDictionary *options = @{
            MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
            MKLaunchOptionsMapTypeKey:
                [NSNumber numberWithInteger:MKMapTypeStandard],
            MKLaunchOptionsShowsTrafficKey:@YES
                };
    
                [MKMapItem openMapsWithItems:mapItems launchOptions:options];
    
            } else {
                //error nothing found
            }
        }];
        return;
    } else {
    
        NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];
    
        NSString *urlToOpen = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
                     [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                     [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
    }
    

    For ios5, the Current Location needs to be in the correct language. I use the LocalizedCurrentLocation from this post http://www.martip.net/blog/localized-current-location-string-for-iphone-apps

    For ios6, I use the CLGeocoder to get the placemark and then open the map with it and the current location.

    Remember to add CoreLocation.framework and MapKit.framework

    0 讨论(0)
  • 2020-12-02 04:49
    NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=Current Location&saddr=%@",startAddr];
    NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [[UIApplication sharedApplication] openURL:url];
    [url release];
    

    This works but only when the iPhone/iPod language is set in English. If you want to support other languages you'll have to use a localized string to match the Maps bookmark name.

    0 讨论(0)
提交回复
热议问题