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.
You can open maps with direction using this code : (assuming your id< MKAnnotation > class has a CLLocationCoordinate2D public property named "coordinate")
You can also change the button, actually you are using a standard style button :
But you can alloc your custom button with an image or a label :
Here is the working code for showing directions on Apple map. It'll work for current place to your destination place and you just need to pass lat & long of destination place.
Also, share me here, if notice any issue or we need to find out another way to done it.
You init a button and add action to button:
Objective-C Code
with the mapPoint is where you want to direction to.
Swift3 or later
Note that
saddr
,daddr
can be location name or location coordinate. SodirectionsURL
can be:More options parameters (like transport type, map type ...) look at here