This article comes as a completion to my previous ones, XCode : using MapKit with no geocoding available out of the box and XCode: Modal windows – Google maps and shows how to add pins on your already created Map View.
First of all, adding the points to the map is not as intuitive as it might seem, so let’s start by creating a new class called POI – point of interest. Its header must be filled with this code:
#import <foundation foundation.h> #import <mapkit mapkit.h> #import <corelocation corelocation.h> @interface POI : NSObject{
CLLocationCoordinate2D coordinate;
NSString *subtitle;
NSString *title;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic,retain) NSString *title;
- (id) initWithCoords:(CLLocationCoordinate2D) coords;
@end
And the implementation should look like this:
#import "POI.h" @implementation POI @synthesize coordinate; @synthesize subtitle; @synthesize title; - (id) initWithCoords:(CLLocationCoordinate2D) coords{ self = [super init]; if (self != nil) coordinate = coords; return self; } - (void) dealloc{ [title release]; [subtitle release]; [super dealloc]; } @end
Then, adding the point is quite easy, as you can see in the following code snippet. Assuming that we have the Latitude and Longitude as NSStrings, we check if they are filled. Then I create the CLLocationCoordonate2D which basically is a tuple of two doubles and then create the point of interest. I could have set also its title and description. Then I removed all annotations already existing, because this map will show only one point.
The next thing that I set was the region – MKCoordinateRegion and span of the view MKCoordinateSpan constructs, which are very intuitive so I will not dig deeper into them.
Finally, by using setRegion method of my map control I set the already filled MKCoordinateRegion and set that the transition and zoom should be animated, to give a nice diving effect. improving user experience. Finally, I tell to the control that it should show only the region of the map that fits my specific area, controlling this way the amount of visual information showed to the user.
Else, if they are 0 I throw an alert to the user.
if (!([Latitude isEqualToString:@"0"] || [Longitude isEqualToString:@"0"] )){ NSLog(Latitude) ; NSLog(Longitude); CLLocationCoordinate2D pinlocation; pinlocation.latitude = [Latitude doubleValue]; pinlocation.longitude = [Longitude doubleValue]; POI *poi = [[POI alloc] initWithCoords:pinlocation]; [mapkitView removeAnnotations:[mapkitView annotations]]; [mapkitView addAnnotation:poi]; [poi release]; /*Region and Zoom*/ MKCoordinateRegion region; MKCoordinateSpan span; span.latitudeDelta= 0.02; span.longitudeDelta= 0.02; region.span=span; region.center=pinlocation; [mapkitView setRegion:region animated:TRUE]; [mapkitView regionThatFits:region]; }else{ UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Can't find address!" message:@"Sorry, the address you input can't be located.Please try another one." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; }