使用CLLocationManager類,MKMapView。並且實現<MKMapViewDelegate,CLLocationManagerDelegate>
//初始化CLLocationManager,CLLocationManager獲得當前地理座標
locmanager=[[CLLocationManager alloc]init];
[locmanager setDelegate:self];
//設定精確度
[locmanagersetDesiredAccuracy:kCLLocationAccuracyBest];
[locmanagerstartUpdatingLocation];
執行完以後,會自動調用代理方法:
在代理方法:
- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation{
//初始化矩形大小
CGRect rect=CGRectMake(0,0,320,460);
//設定地圖大小為矩形大小
map=[[MKMapView alloc] initWithFrame:rect];
CLLocationCoordinate2Dloc=[newLocation coordinate];
lat=loc.latitude;
lon=loc.longitude;
//coordinate座標
CLLocationCoordinate2DtheCoordinate;
CLLocationCoordinate2DtheCenter;
//theCoordinate.latitude=lat;
//theCoordinate.longitude=lon;
theCoordinate=loc;
[map setDelegate:self];
//設定地圖顯示的類型,有衛星地圖,道路,等
[map setMapType:MKMapTypeStandard];
//[mapsetMapType:MKMapTypeSatellite];
//地區座標Region(地區,地區)
MKCoordinateRegiontheRegin;
//theCenter.latitude=lat;
//theCenter.longitude=lon;
theCenter=loc;
theRegin.center=theCenter;
//座標間距(span:間隔,間距)
MKCoordinateSpantheSpan;
theSpan.latitudeDelta=0.1;
theSpan.longitudeDelta=0.1;
//設定地圖顯示的地區,
theRegin.span=theSpan;
//[mapsetRegion:theRegin];
[mapregionThatFits:theRegin];
map.showsUserLocation=YES;
[self.viewaddSubview:map];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapViewviewForAnnotation:(id<MKAnnotation>)annotation{
NSLog(@"-------viewForAnnotation-------");
//此類可以顯示針一樣的表徵圖
MKPinAnnotationView*newAnnotation=[[MKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"annotation1"];
//newAnnotation.animatesDrop=YES;
//newAnnotation.animatesDrop=NO;
newAnnotation.pinColor=MKPinAnnotationColorPurple;
//顯示標誌提示
newAnnotation.canShowCallout=YES;
return newAnnotation;
}