當(dāng)與地圖關(guān)聯(lián)一般是需要獲取用戶的當(dāng)前位置的。MKMapView這個類是在MapKit.framework.這個框架中的。但是獲取用戶的經(jīng)緯度又是另外一個類CLLocationManager這個類在CoreLocation.framework中的。
使用CLLocationManager我們需要設(shè)置 ?CLLocationManagerDelegate 代理。
locationManager= [[CLLocationManager alloc]init];
locationManager.delegate=self;
locationManager.distanceFilter=kCLLocationAccuracyNearestTenMeters;//指定的最小距離米更新。這個是10米
locationManager.desiredAccuracy=kCLLocationAccuracyBest;所需的定位精度?
if([[[UIDevice currentDevice]systemVersion]floatValue] >=8) {
[locationManagerrequestWhenInUseAuthorization];//?
}
[locationManagerstartUpdatingLocation];//開始更新
使用CLLocationManager我們需要在info.plist文件里面配置NSLocationWhenInUseDescription 或者NSLocationAlwaysUsageDescription他們都是字符串類型。就是當(dāng)要用到CLLocationManager要向用戶請求是否允許程序訪問用的位置。上面二個就是描述提示的文字。
- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation ;在這個方法里面我們就可以訪問到用戶的當(dāng)前位置的經(jīng)緯度。
MKMapView繼承的是UIView,只是顯示的時候是地圖的畫面。我們需要把當(dāng)前用戶的位置顯示在地圖上面
_mapview=[[MKMapView alloc]initWithFrame:CGRectMake(0,64,SCREEN_WIDTH,SCREEN_HEIGHT-64)];
_mapview.delegate=self;? ? //MKMapViewDelegate? ? ? ? ? ? ? ? ?
_mapview.userInteractionEnabled=YES;
MKMapView 這樣設(shè)置之后并不能顯示用戶的當(dāng)前位置。
需要實(shí)現(xiàn)代理方法
- (MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation {
MKAnnotationView*annotationView =nil;
if(![annotationisKindOfClass:[MKUserLocation class]])
{
MKAnnotationView*annotationView =[_mapviewdequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation1"];
if(!annotationView) {
annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotation
reuseIdentifier:@"CustomAnnotation1"] ;
annotationView.canShowCallout=NO;
annotationView.image= [UIImageimageNamed:@"mypositionimage"];
} ?//這一部分先忽略,下一篇寫自定義標(biāo)注圖標(biāo)
}
returnannotationView;
}
這樣用戶的當(dāng)前位置如下顯示藍(lán)色的漸變。
