Google Maps directions in maps shared app to personalized annotations
-
22-04-2021 - |
Question
I created an app that displays different points on a custom map with custom pins and showing the title and subtitle with a disclosure button (all right). The problem arises when the app launches maps to create the path and then provide directions. To move from the position of the user I've simply typed in the URL "saddr=Current Position". The problem comes when I try to give the destination that the user has touched, relative to the pin touched.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
[self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f%1.6f&saddr=Posizione Attuale", mapView.annotations.coordinate.latitude mapView.annotations.coordinate.longitude];
//also mapview.annotation.coordinate.latitude/longitude doesn't work
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
I can not figure out how to pass the coordinates of the annotation in the piece of code!
Here's how I said and added the annotation to my view
NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = 45.7;
theCoordinate1.longitude = 7.64;
myAnnotation* myAnnotation1=[[myAnnotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title=@"Pippo";
myAnnotation1.subtitle=@"Ufficio";
[myMapView addAnnotation:myAnnotation1];
This repeated for 4 main points that have different names (myAnnotation1, 2, 3, 4) and coordinates other! How can I do when the user touches 1-2-3 or 4 to move the right destination?
Thanks in advance! :)
Solution
In calloutAccessoryControlTapped
, why are you pushing a blank view controller and calling openURL
at the same time?
Anyway, the annotation's coordinates are in view.annotation.coordinate
.
So the latitude is view.annotation.coordinate.latitude
and longitude is view.annotation.coordinate.longitude
.
Also, in your addr
string, you are missing a comma between the coordinates.