How can I change annotation title on Apple Maps Route - ios

I've created a route from A to B with MKMapItem, but I can't change the title of the annotation on the map. My code:
CLLocation *locationRestaurante = [[CLLocation alloc] initWithLatitude:[[[listaRestaurante objectAtIndex:0] objectForKey:#"latitude"] floatValue] longitude:[[[listaRestaurante objectAtIndex:0] objectForKey:#"longitude"] floatValue]];
MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:locationRestaurante.coordinate addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
NSDictionary *options = #{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
[mapItem openInMapsWithLaunchOptions:options];
it opens everything just fine... But there's only one problem: The I can't change this annotation name:

mapItem.name = #"Place name";
MKMapItem Class Reference

Related

Mapkit route of two locations in mapview not working indian latitude and longtude?

here is my code :
Working :
CLLocationCoordinate2D sourceCoords = CLLocationCoordinate2DMake(37.7833, -122.4167);//san fransisco
MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoords addressDictionary:nil];
MKMapItem *source = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
// Make the destination location
CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(34.0500, -118.2500);//los angeles
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
NotWorking :
CLLocationCoordinate2D sourceCoords = CLLocationCoordinate2DMake(13.0441, +80.1831);
MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoords addressDictionary:nil];
MKMapItem *source = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
Make the destination location CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(13.0545, +80.2114);
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
polyline not showing in mapview .
Apple maps is not supporting Directions in india. you have to use Google Maps for Directions in india. you can check map feature availability here

How to search for a destination using name in MapKit?

I want to search for a destination using the name in MapKit and get back the longitude and latitude (CLLocationCoordinate2D).
Currently I'm using the hardcoded value to set the destination.
// Make a directions request
MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
// Start at our current location
MKMapItem *source = [MKMapItem mapItemForCurrentLocation];
[directionsRequest setSource:source];
// Make the destination --> I WANT TO GET THIS COORDINATE USING NAME
CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(45.545824, 9.327515);
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
[directionsRequest setDestination:destination];
You have to use CLGeocoder to forward-geocode using an address:
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:#"New York City" completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(#"%#", error);
} else {
CLPlacemark *placemark = [placemarks lastObject];
MKCoordinateRegion region;
region.center.latitude = placemark.location.coordinate.latitude;
region.center.longitude = placemark.location.coordinate.longitude;
[self.mapView setRegion:region animated:YES];
}
}];
This will set the map's region to the location returned, using the latitude and longitude as it's centre.

Setting the title and subtitle on a Map Annotation view

I followed this question: iOS - MKMapView place annotation by using address instead of lat / long - to create a map annotation for a postal code as opposed to the long/lat values directly.
This works fine, however I would like to set the title and subtitle of the anno
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
placemark.title = self.business.businessName;
placemark.subtitle = self.business.phoneNumber;
This is not working as the title and subtitle are readonly. How can I change the above so that I am able to set the title and subtitle?
Use MKPointAnnotation instead.
Sample Code :
CLPlacemark *topresult = [placemarks objectAtIndex:0];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = topresult.location.coordinate;
annotation.title = self.business.businessName;
annotation.subtitle = self.business.phoneNumber;
[self.mapView addAnnotation:annotation];
You can try below code. It may help you.
//set the title if we got any placemarks...
if (placemark.count > 0)
{
CLPlacemark *topResult = [placemark objectAtIndex:0];
annTitle = [NSString stringWithFormat:#"%# %# %# %#", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare];
}
//now create the annotation...
MapAnnotation *toAdd = [[MapAnnotation alloc]init];
toAdd.coordinate = touchMapCoordinate;
toAdd.title = #"Address";
toAdd.subtitle = #"Sub Address";
[self.map addAnnotation:toAdd];

iOS map view is missing the pin for a location

I got a very weird problem when I tried to call iOS map for navigation from current location to a location in my app. The routes were correctly calculated, but the destination location doesn't have a pin in the map, the current location has the pin though. My code is like the following. I think I'm missing something really basic but can't figure out.... Any thoughts?
(void) routeInMap
{
CLLocationCoordinate2D location;
location.latitude = [[thisAttraction objectForKey:#"lat"] floatValue];
location.longitude = [[thisAttraction objectForKey:#"lng"] floatValue];
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:[thisAttraction objectForKey:#"placeAddress"]
completionHandler:^(NSArray *placemarks, NSError *error) {
// Convert the CLPlacemark to an MKPlacemark
// Note: There's no error checking for a failed geocode
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithCoordinate:location
addressDictionary:geocodedPlacemark.addressDictionary];
// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeWalking instead
NSDictionary *launchOptions = #{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:#[currentLocationMapItem, mapItem] launchOptions:launchOptions];
}];
}
}

Get direction through Map doesn't work with iOS 7

I calling this method to direct the user on Apple Map. It was working fine with iOS 6 but now it is not working with iOS 7. So please where it would be the issue?
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
lat1 = [[view annotation]coordinate].latitude;
lon1 = [[view annotation]coordinate].longitude;
NSString *str = [NSString stringWithFormat:#"http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f", lat1,lon1,lat,lon];
NSURL *URL = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:URL];
}
UPDATE
-(void)direct:(id)sender{
CLLocationCoordinate2D coordinate;
coordinate.latitude = 40.896056;
coordinate.longitude = 29.217947;
MKPlacemark *mPlacemark = [[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil] autorelease];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:mPlacemark];
[mapItem openInMapsWithLaunchOptions:#{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving}];
}
You should use MKPlacemark on iOS 6 and above.
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:[view annotation]coordinate]] addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem openInMapsWithLaunchOptions:#{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving}];

Resources