Following are the topics which are covered in this post
- Display a specific place in iOS Map
- Display multiple places in iOS Map
- Get Directions from your location to a specific place
- Get Directions from a Custom Origin
Before we start add the following frameworks and import them
- MapKit
- AddressBook
Lets Get Started ..
Display a specific place in iOS Map
Its very simple to display a place (pin) in iOS Map. All you need is to follow 3 simple steps
1. Create an instance of MKPlaceMark with the coordinates and address dictionary
MKPlacemark *placeMark = [[MKPlacemark alloc]initWithCoordinate:coordinate addressDictionary:addressDict];
2. Create an instance of MKMapItem with the PlaceMark
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark];
3. Launch the iOS map using its instance method ‘openInMapsWithLaunchOptions:‘ and thats it..
[mapItem openInMapsWithLaunchOptions:nil];
Here is the sample code.. 
CLLocationCoordinate2D coordinate;
coordinate.latitude = 48.8580;
coordinate.longitude = 2.29460;
NSDictionary *addressDict = @{(NSString*)kABPersonAddressStreetKey:@”Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France”};
MKPlacemark *placeMark = [[MKPlacemark alloc]initWithCoordinate:coordinate addressDictionary:addressDict];
coordinate.latitude = 48.8580;
coordinate.longitude = 2.29460;
NSDictionary *addressDict = @{(NSString*)kABPersonAddressStreetKey:@”Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France”};
MKPlacemark *placeMark = [[MKPlacemark alloc]initWithCoordinate:coordinate addressDictionary:addressDict];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark];
[mapItem setName:@"Eiffel Tower"];
[mapItem setPhoneNumber:@"999-999-9999"];
[mapItem setUrl:[NSURL URLWithString:@"http://jprithvi.wordpress.com"]];
[mapItem openInMapsWithLaunchOptions:nil];
[mapItem setName:@"Eiffel Tower"];
[mapItem setPhoneNumber:@"999-999-9999"];
[mapItem setUrl:[NSURL URLWithString:@"http://jprithvi.wordpress.com"]];
[mapItem openInMapsWithLaunchOptions:nil];
Display multiple places in iOS Map
To display multiple places, get the instance of the places (MKMapItems) , group it in a NSArray and launch the map using MKMapItem’s Class Method ‘openMapsWithItems:launchOptions:‘. Its done..
Here is the sample code.. 
CLLocationCoordinate2D coordinate;
coordinate.latitude = 48.8580;
coordinate.longitude = 2.29460;
NSDictionary *addressDict = @{(NSString*)kABPersonAddressStreetKey:@”Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France”};
MKPlacemark *placeMark = [[MKPlacemark alloc]initWithCoordinate:coordinate addressDictionary:addressDict];
coordinate.latitude = 48.8580;
coordinate.longitude = 2.29460;
NSDictionary *addressDict = @{(NSString*)kABPersonAddressStreetKey:@”Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France”};
MKPlacemark *placeMark = [[MKPlacemark alloc]initWithCoordinate:coordinate addressDictionary:addressDict];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark];
[mapItem setName:@"Eiffel Tower"];
[mapItem setPhoneNumber:@"999-999-9999"];
[mapItem setUrl:[NSURL URLWithString:@"http://jprithvi.wordpress.com"]];
[mapItem setName:@"Eiffel Tower"];
[mapItem setPhoneNumber:@"999-999-9999"];
[mapItem setUrl:[NSURL URLWithString:@"http://jprithvi.wordpress.com"]];
CLLocationCoordinate2D coordinate2;
coordinate2.latitude = 48.8044;
coordinate2.longitude = 2.1232;
NSDictionary *addressDict2 = @{(NSString*)kABPersonAddressStreetKey:@”Place d’Armes, 78000 Versailles, France”};
MKPlacemark *placeMark2 = [[MKPlacemark alloc]initWithCoordinate:coordinate2 addressDictionary:addressDict2];
coordinate2.latitude = 48.8044;
coordinate2.longitude = 2.1232;
NSDictionary *addressDict2 = @{(NSString*)kABPersonAddressStreetKey:@”Place d’Armes, 78000 Versailles, France”};
MKPlacemark *placeMark2 = [[MKPlacemark alloc]initWithCoordinate:coordinate2 addressDictionary:addressDict2];
MKMapItem *mapItem2 = [[MKMapItem alloc] initWithPlacemark:placeMark2];
[mapItem2 setName:@"Palace of Versailles"];
[mapItem2 setPhoneNumber:@"999-999-9999"];
[mapItem2 setUrl:[NSURL URLWithString:@"http://jprithvi.wordpress.com"]];
[mapItem2 setName:@"Palace of Versailles"];
[mapItem2 setPhoneNumber:@"999-999-9999"];
[mapItem2 setUrl:[NSURL URLWithString:@"http://jprithvi.wordpress.com"]];
NSArray *mapPoints = @[mapItem , mapItem2];
[MKMapItem openMapsWithItems:mapPoints launchOptions:nil];
[MKMapItem openMapsWithItems:mapPoints launchOptions:nil];
Now What is the use of Launch Options?
We can set various properties in launchOption dictionary such as MapType – Standard/Satellite/Hybrid, Custom Region, Traffic or Direction Modes – Wakling/Driving, to change the map settings.
Get Directions from your location to a specific place
To get the direction we set the direction mode in launchOption and launch the map with MKMapItem’s instance method. Map understands and plots the route from your location to the specified place. 
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,};
[mapItem openInMapsWithLaunchOptions:launchOptions];
[mapItem openInMapsWithLaunchOptions:launchOptions];
Get Directions from a Custom Origin
To get the directions between two places other than your location, group the instances of MKMapItem in a NSArray like we did to launch the map with multiple places and then launch the map with MKMapItem’s Class method. Map understands the call and plots the direction from first item to second item. 
NSArray *mapPoints = @[sourceItem , destinationItem];
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,};
[MKMapItem openMapsWithItems:mapPoints launchOptions:launchOptions];}
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,};
[MKMapItem openMapsWithItems:mapPoints launchOptions:launchOptions];}
Wow Thanks to MKMapItem., Our Job is too easy.. Have Fun
コメント
コメントを投稿