CoreLocation+MapKit系统定位(含坐标以及详细地址)
生活随笔
收集整理的这篇文章主要介绍了
CoreLocation+MapKit系统定位(含坐标以及详细地址)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
iOS8 之后出现一些新的配置
[self.manager requestWhenInUseAuthorization];并且在info.plist文件中增加NSLocationWhenInUseUsageDescription BOOL YESNSLocationAlwaysUsageDescription string “提示描述”记得加依赖库
CoreLocation.framework MapKit.framework
创建MapView
if (_mapView == nil) {_mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 0.5, 0.5)];// _mapView.userTrackingMode = MKUserTrackingModeFollow;_mapView.delegate = self;_mapView.showsUserLocation = YES;[self.view addSubview:_mapView]; }创建locationManager
if ([CLLocationManager locationServicesEnabled] == YES) {//判断定位是否可用_locationManager = [[CLLocationManager alloc]init];_locationManager.delegate = self;_locationManager.desiredAccuracy = kCLLocationAccuracyBest;//定位精确度_locationManager.distanceFilter = 10; //位置变化10米更新位置信息//NSLog(@"定位");if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)[_locationManager requestWhenInUseAuthorization]; }else{//NSLog(@"定位不可用"); }[_locationManager startUpdatingLocation];CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{//NSLog(@"%s",__FUNCTION__);[manager stopUpdatingLocation];NSLog(@"newLocation = %@",newLocation);NSLog(@"oldLocation = %@",oldLocation);//获取经度和纬度的结构体CLLocationCoordinate2D coordinate = newLocation.coordinate;//纬度信息,CLLocationDegrees类型实际上就是double型CLLocationDegrees latitude = coordinate.latitude;//经度信息,CLLocationDegrees类型实际上就是double型CLLocationDegrees longitude = coordinate.longitude;NSLog(@"%f,%f",latitude,longitude);[self.latitude setText:[NSString stringWithFormat:@"纬度:%lf",latitude]];[self.longitude setText:[NSString stringWithFormat:@"经度:%lf",longitude]];/*// 获取当前所在的城市名CLGeocoder *geocoder = [[CLGeocoder alloc] init];//根据经纬度反向地理编译出地址信息[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){if (array.count > 0){CLPlacemark *placemark = [array objectAtIndex:0];//详细信息[self.address setText:[NSString stringWithFormat:@"地址:%@",placemark.name]];//获取城市NSString *city = placemark.locality;if (!city) {//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)city = placemark.administrativeArea;}[self.city setText:[NSString stringWithFormat:@"城市:%@",city]];}else if (error == nil && [array count] == 0){NSLog(@"No results were returned.");}else if (error != nil){NSLog(@"An error occurred = %@", error);}}];*/ }-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{NSLog(@"定位失败 ,%@",error); }MapView获得详细地址
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{CLLocation * newLocation = userLocation.location;self.lastCoordinate=mapView.userLocation.location.coordinate;NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];[standard setObject:@(self.lastCoordinate.longitude) forKey:MMLastLongitude];[standard setObject:@(self.lastCoordinate.latitude) forKey:MMLastLatitude];CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error){for (CLPlacemark * placeMark in placemarks){NSDictionary *addressDic=placeMark.addressDictionary;NSString *City=[addressDic objectForKey:@"City"]; // NSString *subLocality=[addressDic objectForKey:@"SubLocality"]; // NSString * adressName = [NSString stringWithFormat:@"%@%@",subLocality,street];[self.city setText:[NSString stringWithFormat:@"城市:%@",City]];[self.address setText:[NSString stringWithFormat:@"地址:%@",placeMark.name]];[self stopLocation];}};[[NSUserDefaults standardUserDefaults] synchronize];[clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];}停止定位
-(void)stopLocation {if (_mapView) {NSLog(@"关闭");_mapView.showsUserLocation = NO;[_mapView removeFromSuperview];_mapView = nil;} }
来源: http://www.cnblogs.com/spaceID/p/4992167.html
转载于:https://www.cnblogs.com/spaceID/p/4992167.html
总结
以上是生活随笔为你收集整理的CoreLocation+MapKit系统定位(含坐标以及详细地址)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: (算法)跳跃问题
- 下一篇: Windows 10 不能安装更新 (