.h file #import <MapKit/MapKit.h> #define METERS_PER_MILE 1609.344 @interface mymapViewController : UIViewController <MKMapViewDelegate,MKAnnotation,CLLocationManagerDelegate> { CLLocationManager *locationManager; IBOutlet MKMapView *map; } .m file - (void)viewDidLoad { [super viewDidLoad]; //CLLocationCoordinate2D zoomLocation; // zoomLocation.latitude = 23.0000; // zoomLocation.longitude = 72.4000; // // 2 // MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 500000, 500000); // // 3 // MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion]; // // 4 // [map setRegion:adjustedRegion animated:YES]; map.showsUserLocation = YES; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; [locationManager startUpdatingLocation]; //float a = 23.000; // float b = 73.000; NSMutableArray * arr = [[NSMutableArray alloc]init]; [arr addObject:@"23.000"]; [arr addObject:@"73.000"]; [arr addObject:@"73.000"]; [arr addObject:@"23.000"]; CLLocationCoordinate2D coordinate; coordinate.latitude = locationManager.location.coordinate.latitude; coordinate.longitude = locationManager.location.coordinate.longitude; MyLocation *annotation = [[[MyLocation alloc] initWithName:@"current" address:@"sas" coordinate:coordinate] autorelease]; [map addAnnotation:annotation]; for (int i = 0 ; i < 2; i++) { CLLocationCoordinate2D coordinate1; coordinate1.latitude = [[arr objectAtIndex:i] floatValue]; coordinate1.longitude = [[arr objectAtIndex:i+1] floatValue]; MyLocation *annotation1 = [[[MyLocation alloc] initWithName:@"hello" address:@"sas1" coordinate:coordinate1] autorelease]; [map addAnnotation:annotation1]; } MKCoordinateRegion region; MKCoordinateSpan span; span.latitudeDelta=1.1; span.longitudeDelta=1.1; region.span=span; region.center=CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude); [map setRegion:region animated:TRUE]; } - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *identifier = @"MyLocation"; if ([annotation isKindOfClass:[MyLocation class]]) { MyLocation *location = (MyLocation *) annotation; MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:identifier]; if (annotationView == nil) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; } else { annotationView.annotation = annotation; } annotationView.enabled = YES; annotationView.canShowCallout = YES; //if ([location.name compare:@"hello"] == NSOrderedSame) { // annotationView.pinColor = MKPinAnnotationColorGreen; // }else { // annotationView.pinColor = MKPinAnnotationColorPurple; // } return annotationView; } return nil; } Mylocation.h #import <MapKit/MapKit.h> @interface MyLocation : NSObject <MKAnnotation> { NSString *_name; NSString *_address; CLLocationCoordinate2D _coordinate; } @property (copy) NSString *name; @property (copy) NSString *address; @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; - (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate; Mylocation.m @synthesize name = _name; @synthesize address = _address; @synthesize coordinate = _coordinate; - (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate { if ((self = [super init])) { _name = [name copy]; _address = [address copy]; _coordinate = coordinate; } return self; } - (NSString *)title { return _name; } - (NSString *)subtitle { return _address; } - (void)dealloc { [_name release]; _name = nil; [_address release]; _address = nil; [super dealloc]; } @end
Ads