Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opens apple maps app from ios app with directions

Tags:

As the title states I would like to open the native maps app in the ios device from within my own app, by pressing a button. currently I have used an MKmapview that displays a simple pin with lat/long taken from a json file.

the code is this :

- (void)viewDidLoad {     [super viewDidLoad];  }   // We are delegate for map view self.mapView.delegate = self;  // Set title self.title = self.location.title;  // set texts... self.placeLabel.text = self.location.place;   self.telephoneLabel.text = self.location.telephone; self.urlLabel.text = self.location.url;   **// Make a map annotation for a pin from the longitude/latitude points MapAnnotation *mapPoint = [[MapAnnotation alloc] init]; mapPoint.coordinate = CLLocationCoordinate2DMake([self.location.latitude doubleValue], [self.location.longitude doubleValue]); mapPoint.title = self.location.title;**  // Add it to the map view [self.mapView addAnnotation:mapPoint];  // Zoom to a region around the pin MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500); [self.mapView setRegion:region]; 

}`

When you touch the pin an info box appears with a title and an info button.

this is the code :

    #pragma mark - MKMapViewDelegate  - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {     MKPinAnnotationView *view = nil;     static NSString *reuseIdentifier = @"MapAnnotation";      // Return a MKPinAnnotationView with a simple accessory button     view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];     if(!view) {         view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];         view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];         view.canShowCallout = YES;         view.animatesDrop = YES;     }      return view; } 

I want to make a method that opens the maps app with directions from the Current users location to the mapPoint above , when clicking the button in the info box above. Is that possible? Also Can I customize the look of this button? (i mean like putting a different image to the button to make it look like "press me for direction kinda" ).

Sorry if this a stupid question, but this is my first ios app and Obj-c is a totally new language to me.

thanks for all the replies in advance.

like image 631
Kostenko Avatar asked Feb 24 '14 09:02

Kostenko


People also ask

How do I get directions on my iPhone Maps app?

Get directions for driving Tap your destination (such as a search result in Maps or a landmark on a map), or touch and hold anywhere on the map, then tap the directions button.

How do I change Apple Maps settings?

In the Maps app on your Mac, choose Maps > Preferences, then click a preference pane. General: Change the appearance of your maps—for example, you can include weather and air quality information, and a compass. Route Planner: Change how you want to get around while driving, while cycling, or taking transit.


1 Answers

Here is code to open Map app with directions:

Objective-C Code

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

with the mapPoint is where you want to direction to.

Swift3 or later

let directionsURL = "http://maps.apple.com/?saddr=35.6813023,139.7640529&daddr=35.4657901,139.6201192" guard let url = URL(string: directionsURL) else {     return } if #available(iOS 10.0, *) {     UIApplication.shared.open(url, options: [:], completionHandler: nil) } else {     UIApplication.shared.openURL(url) } 

Note that saddr, daddr is urlencoded string of location name or location coordinate (about encode URL look at here).

directionsURL example:

// directions with location coordinate "http://maps.apple.com/?saddr=35.6813023,139.7640529&daddr=35.4657901,139.6201192" // or directions with location name "http://maps.apple.com/?saddr=Tokyo&daddr=Yokohama" // or directions from current location to destination location "http://maps.apple.com/?saddr=Current%20Location&daddr=Yokohama" 

More options parameters (like transport type, map type ...) look at here

like image 186
larva Avatar answered Sep 18 '22 03:09

larva