iOS定位(一)地图定位

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

带地图的定位方法

#import<CoreLocation/CoreLocation.h>#import<MapKit/MapKit.h>

<MKMapViewDelegate>//1.使用定位服务//设置app有访问定位服务的权限//在使用应用期间 / 始终(app在后台)//info.plist文件添加以下两条(或者其中一条):这句话非常重要,关系到你能不能定位//NSLocationWhenInUseUsageDescription 在使用应用期间

//NSLocationAlwaysUsageDescription 始终//2.LocationManager 对象管理相关的定位服务_manager =[[CLLocationManager alloc] init];//manager判断: 手机是否开启定位 / app是否有访问定位的权限//[CLLocationManager locationServicesEnabled];//手机是否开启定位//[CLLocationManager authorizationStatus];//app访问定位的权限的状态if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] !=kCLAuthorizationStatusAuthorizedWhenInUse) {

[_manager requestWhenInUseAuthorization];//向用户请求访问定位服务的权限}//3.地图[self createMapView];- (void)createMapView

{//创建MapView_mapView =[[MKMapView alloc] initWithFrame:self.view.bounds];//地图的类型_mapView.mapType =MKMapTypeStandard;//显示用户当前的位置//_mapView.userLocation//是否定位(是否追踪用户的位置)_mapView.userTrackingMode =MKUserTrackingModeFollow;

_mapView.delegate=self;

[self.view addSubview:_mapView];

}//更新用户的位置- (void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation*)userLocation

{

NSLog(@"location : %@", userLocation.location);//根据经纬度设置MapView//设置显示的区域范围, 设置显示的精度//设置显示的精度(比例尺)MKCoordinateSpan span = MKCoordinateSpanMake(0.01,0.01);//指定的范围MKCoordinateRegion region =MKCoordinateRegionMake(userLocation.location.coordinate, span);//移动mapView显示的位置[_mapView setRegion:region animated:YES];

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航