1. 導(dǎo)入地圖框架,設(shè)置代理方法
#import <CoreLocation/CoreLocation.h>
#import<MapKit/MapKit.h>
MKMapViewDelegate,CLLocationManagerDelegate
2. 創(chuàng)建mapView
MKMapView*mapView = [[MKMapView alloc]initWithFrame:CGRectZero];
mapView.mapType=MKMapTypeStandard;
mapView.delegate=self;
[superView addSubview:mapView];
3. 初始化LocationManager
if(nil==_locaManager) {
? ? ? ? ? ?_locaManager= [[CLLocationManager alloc]init];
}
if([SystemVersion floatValue]>=8.0) {
? ? ? ? ? ? [_locaManager requestWhenInUseAuthorization];
? ? ? ? ?// [_locaManager requestAlwaysAuthorization];
}
//設(shè)置代理
? [_locaManager setDelegate:self];
//設(shè)置位置的精度
[_locaManager setDesiredAccuracy:kCLLocationAccuracyBest];
//是指多遠(yuǎn)才更新位置信息設(shè)置距離過濾器(多少距離開始更新定位,如下面10米之內(nèi)就不改變定位)
[_locaManager setDistanceFilter:5.0f];
////授權(quán)一直開啟定位服務(wù)(申請(qǐng)授權(quán);ios8需要加配置;省電考慮;7以前直接調(diào)用申請(qǐng)授權(quán)的方法)
//[_locaManager requestAlwaysAuthorization];
//還有一種方法requestWhenInUseAuthorization(需要才開啟);這兩中方法選擇時(shí),其在plist中配置的要與之對(duì)應(yīng)
mapView.showsUserLocation=YES;//根據(jù)定位的位置標(biāo)出來(藍(lán)色的圈)
//開啟定位服務(wù)(開始更新位置信息)
[_locaManager startUpdatingLocation];
//如果不需要實(shí)時(shí)定位,使用完即使關(guān)閉定位服務(wù)[myLocationManager stopUpdatingLocation];
4. 代理方法
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{
? ? ? ? ?//停止獲取位置信息
? ? ? ? ?[self.locaManager stopUpdatingLocation];
? ? ? ? ?//定位關(guān)閉
? ? ? ? ?_mapView.showsUserLocation=NO;
? ? ? ? ? ? ?//位置信息(NSArray *)locations是一個(gè)數(shù)組;因?yàn)椴灰欢ㄖ豢縂PS定 ?位,還可通過基站,WiFi熱點(diǎn)等這些輔助來定位
? ? ? ? ? ?//提示:這里獲得的定位信息是地球坐標(biāo)
? ? ? ? ? ? //如果不轉(zhuǎn)換成火星坐標(biāo)那么在蘋果原生地圖上定位就會(huì)有問題
? ? ? ? ? ?//獲取位置對(duì)象
? ? ? ? ? ?CLLocation*lastLocation = [locations lastObject];
? ? ? ? ?//使用第三方庫將地球坐標(biāo)轉(zhuǎn)換成火星坐標(biāo)
? ? ? ? ?//lastLocation = [lastLocation locationMarsFromEarth];
? ? ? ?//取出當(dāng)前位置的坐標(biāo)
? ? ? ? [self ?searchLocationDataWithCLLocation:lastLocation];
? ? ? ?//提取位置信息里的經(jīng)度,緯度
? ? ? ? CLLocationCoordinate2D myLocation= [lastLocation coordinate];
? ? ? ? CLLocationDegrees ?dinglatitude =myLocation.latitude;
? ? ? ? CLLocationDegrees dinglongitude = myLocation.longitude;
}
#pragma mark -搜索城市name
- (void)searchLocationDataWithCLLocation:(CLLocation*)coordinate
{
? ? ? ?//ios >=5
? ? ? ? ? CLGeocoder*geocoder = [[CLGeocoder alloc]init];
? ? ? ? ? [geocoder reverseGeocodeLocation:coordinate completionHandler:^(NSArray *_Nullable placemarks,NSError*_Nullable error){
? ? ? ? ? if(placemarks.count>0) {
? ? ? ? ? ? ? ? CLPlacemark*placemark = [placemarks firstObject];
? ? ? ? ? ? ? ? NSString*country = placemark.ISOcountryCode;
? ? ? ? ? ? ? ? NSString*country1 = placemark.country;
? ? ? ? ? ? ? ? ?NSString*city = placemark.locality;
? ? ? ? ? ? ? ? ? ? ?if(!city) {
? ? ? ? ? ? ? ? ? ? ? ? ?//四大直轄市的城市信息無法通過locality獲得,只能通過獲取省份的方法來獲得(如果city為空,則可知為直轄市)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?city = placemark.administrativeArea;
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ?}];
}