スキップしてメイン コンテンツに移動

Launch iOS Map with MKMapItem


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];
PinDetail
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];
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];
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..
2Pins
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];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark];
[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];
MKMapItem *mapItem2 = [[MKMapItem alloc] initWithPlacemark:placeMark2];
[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];
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];
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];}
Wow Thanks to MKMapItem., Our Job is too easy..  Have Fun

コメント

このブログの人気の投稿

日本に来た 今三週間

四月六日に日本に来た、今三週間になった。 嬉しくなかった、悲しくなかった。 日本の携帯電話のカメラはとてもいいよ、私は今撮影が好きだ、いつも寮に帰る途中で花とか魚とか写真を撮る。これも生活の趣味だ。 滴は綺麗だ 桜が凋んで落ちる 天気がいいとき、自転車に乗って、食材を買った。 自分で料理をつくる、おいしそうでしょう。 昨日清水寺に行った、天気はとても熱いね。日に焼けすぎたので、腕は今赤い。 側に花があるので、赤い清水寺はもっと美しそうだ。 木の色は艶やかだ。