日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

CoreLocation+MapKit系统定位(含坐标以及详细地址)

發布時間:2025/3/21 windows 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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系统定位(含坐标以及详细地址)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。