(1)首先拉一個mapView到你的工程
(2)在你的Controller.h中加入:
@interface EXViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>{@private MKMapView *_mapView; CLLocationManager *_loactionManager;}@property(nonatomic, readwrite,retain) IBOutlet MKMapView *mapView;@property(nonatomic, readwrite, retain)CLLocationManager *locationManager;@end
記得
IBOutlet
一定要加的,而且要串連你的xib中的mapview到這個屬性定義,不然會有麻煩的呵呵。
那麼Controller.m中的實現的代碼:
在viewDidLoad函數中
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. self.mapView.delegate = self; self.mapView.showsUserLocation = YES; self.locationManager = [[[CLLocationManager alloc] init] autorelease]; [self.locationManager startUpdatingHeading]; [self.locationManager startUpdatingLocation]; // }
並且實現函數:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
這個函數用來實現你的地圖顯示,並且可以通過中的那個定位器號來定位自己的位置
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"snda.pin"]; if (annView == NULL) { annView = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"snda.pin"] autorelease]; } annView.annotation = annotation; annView.canShowCallout = YES; annView.calloutOffset = CGPointMake(0, 0); annView.image = [UIImage imageNamed:@"capture-exposure-plus.png"]; annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; return annView;}
和函數
- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ //不斷擷取你的精度和新的地址}