ios自帶的MKMapView學(xué)習(xí)

當(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)色的漸變。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容