Objective-C之定位&地图
来源:互联网 发布:淘宝秒杀群运营系统 编辑:程序博客网 时间:2024/06/06 12:28
转载注明出处,不做抄袭党。
首先要记得调用MapKit
#import <MapKit/MapKit.h>
创建视图就不说了,下面介绍检测定位服务是否开启
if ([CLLocationManager locationServicesEnabled]) { _locationManager = [[CLLocationManager alloc] init]; if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [_locationManager requestWhenInUseAuthorization]; } //设置代理 [_locationManager setDelegate:self]; //设置精度 [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; //设置距离筛选 [_locationManager setDistanceFilter:10]; //开始定位 [_locationManager startUpdatingLocation]; }else{ UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"定位没有开启" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; }}
定位服务开启后我们便可以开始定位
//定位成功- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ CLLocation * location = locations.lastObject; [self reverseGeocoder:location]; MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), MKCoordinateSpanMake(0.1, 0.1)); [_mapView setRegion:[_mapView regionThatFits:coordinateRegion] animated:YES];}//定位失败- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{}
反地理编码代码如下
- (void)reverseGeocoder:(CLLocation *)currentLocation { CLGeocoder * geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if(error || placemarks.count == 0){ NSLog(@"error"); }else{ CLPlacemark * placemark = placemarks.firstObject; // MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude), MKCoordinateSpanMake(0.1, 0.1)); [_mapView setRegion:[_mapView regionThatFits:coordinateRegion] animated:YES]; MKPointAnnotation * pointAnnotation = [[MKPointAnnotation alloc] init]; [pointAnnotation setTitle:placemark.name]; [pointAnnotation setCoordinate:CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)]; [_mapView addAnnotation:pointAnnotation]; NSLog(@"placemark:%@",[[placemark addressDictionary] objectForKey:@"city"]); } }];}
地理编码代码如下
- (void)geocoder:(NSString *)str { CLGeocoder * geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (error || placemarks.count == 0) { NSLog(@"error"); }else{ CLPlacemark * placemark = placemarks.firstObject; MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude), MKCoordinateSpanMake(0.1, 0.1)); [_mapView setRegion:[_mapView regionThatFits:coordinateRegion] animated:YES]; MKPointAnnotation * pointAnnotation = [[MKPointAnnotation alloc] init]; [pointAnnotation setTitle:placemark.name]; [pointAnnotation setCoordinate:CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)]; [_mapView addAnnotation:pointAnnotation]; } }];}
0 0
- Objective-C之定位&地图
- 百度地图之定位
- 百度地图之定位
- 百度地图之定位
- 百度地图之定位
- 百度地图之定位
- 百度地图 之 定位
- 百度地图之定位
- 浏览器定位之 - 百度定位地图显示
- Android 百度地图开发之地图定位
- 地图定位之3D 地图
- 百度地图 之基础地图和定位
- 百度地图之简单定位
- 百度地图之自我定位
- Baidu地图之浏览器定位
- 百度地图之定位,最新版
- 百度地图之定位数据
- 百度地图之SDK定位
- 拒绝从入门到放弃_《Openstack 设计与实现》必读目录
- Fibonacci数列(矩阵乘法快速幂)
- Intel SGX 技术初探
- 第三章 例题7(乒乓比赛)基础树状数组
- android手机的传感器调用
- Objective-C之定位&地图
- SpringMVC创建一个HelloWorld程序
- PAT(A) - 1076. Forwards on Weibo (30)
- Zephys OS nano 内核篇:task 服务 - 基础
- 初试cnn(1)
- 第7届山东省赛sdut 3566---Triple Nim
- 如何使用redis做mysql的缓存
- 深入理解Java:SimpleDateFormat安全的时间格式化
- Zephys OS nano 内核篇:fiber 服务 - 基础