ios系统地图定位及编码和反编码,热点检索
///////
定位
//定位的框架
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
CLLocationManager * locationManager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//定位管理器
locationManager = [[CLLocationManager alloc]init];
// 设置精度 ,精确度越高越费电
// extern const CLLocationAccuracy kCLLocationAccuracyBest;
// extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
// extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
// extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
// extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
locationManager.desiredAccuracy = kCLLocationAccuracyBest ;
//每隔多少米定一次位置
locationManager.distanceFilter = 100;
//iOS 8 必须要请求定位权限,还得在info.plist文件中增加对应权限的key。
//一直定位权限 NSLocationAlwaysUsageDescription
//使用时定位权限NSLocationWhenInUseUsageDescription
// [locationManager requestAlwaysAuthorization];
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
//请求定位权限(iOS8以后才出现的方法) 需要判断版本号,
[locationManager requestWhenInUseAuthorization];
}
//设置代理
locationManager.delegate = self;
//启动定位
[locationManager startUpdatingLocation];
}
//用户更新的位置
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"%@",locations);
//拿到用户的位置
CLLocation *currentLocation = [locations lastObject];
//经纬度信息
NSLog(@"%f %f",currentLocation.coordinate.longitude,currentLocation.coordinate.latitude);
}
/////////////////
编码和反编码
//定位的框架
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
CLLocationManager * locationManager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//定位管理器
locationManager = [[CLLocationManager alloc] init];
// 设置精度 ,精确度越高越费电
// extern const CLLocationAccuracy kCLLocationAccuracyBest;
// extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
// extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
// extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
// extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
locationManager.desiredAccuracy = kCLLocationAccuracyBest ;
//每隔多少米定一次位置
locationManager.distanceFilter = 100;
//iOS 8 必须要请求定位权限,还得在info.plist文件中增加对应权限的key。
//一直定位权限 NSLocationAlwaysUsageDescription
//使用时定位权限NSLocationWhenInUseUsageDescription
// [locationManager requestAlwaysAuthorization];
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
//请求定位权限(iOS8以后才出现的方法) 需要判断版本号,
[locationManager requestWhenInUseAuthorization];
}
//设置代理
locationManager.delegate = self;
//启动定位
[locationManager startUpdatingLocation];
//地理编码
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:@"河南省郑州二七区二七塔" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error != nil || placemarks.count == 0) {
NSLog(@"编码失败了");
return ;
}
CLPlacemark *pm = [placemarks lastObject];
// pm.location.coordinate.latitude;
NSLog(@"经纬度 %f %f",pm.location.coordinate.longitude,pm.location.coordinate.latitude);
}];
}
//用户更新的位置
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"%@",locations);
//拿到用户的位置
CLLocation *currentLocation = [locations lastObject];
//经纬度信息
NSLog(@"%f %f",currentLocation.coordinate.longitude,currentLocation.coordinate.latitude);
// 地理编码 (中文地址信息-》经纬度)和反地理编码(经纬度-》详细地址信息)
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
//反地理编码
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//编码结果 CLPlacemark(地标,位置标识)
if (error != nil || placemarks.count == 0) {
NSLog(@"编码失败");
return ;
}
// NSLog(@"%@",placemarks);
CLPlacemark *placeMark = [placemarks lastObject];
NSLog(@"(市)%@%@ (省)%@ 详细的地址信息%@",placeMark.locality ,placeMark.addressDictionary,placeMark.administrativeArea,placeMark.name);
}];
}
热点检索
//搜索按钮事件
- (void)btnSearchPressed:(UIButton*)btn {
//创建热点搜索(POI检索)的请求
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init] ;
//设置搜索关键字
request.naturalLanguageQuery = btn.currentTitle;
//设置搜索范围
request.region = self.mapView.region;
//创建搜索对象
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error != nil || response.mapItems.count == 0) {
NSLog(@"没有搜索结果");
return ;
}
[response.mapItems enumerateObjectsUsingBlock:^(MKMapItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.placemark;
//热点名称,一家公司 店铺
obj.name;
NSLog(@"%@",obj.name);
//电话%
obj.phoneNumber;
MyAnnotation *annotation = [[MyAnnotation alloc] init];
annotation.coordinate = obj.placemark.coordinate;
annotation.title = obj.name;
//title 酒店
if ([btn.currentTitle isEqualToString:@"酒店"]) {
annotation.imageName = @"category_3";
}
[self.mapView addAnnotation:annotation];
}];
}];
}

浙公网安备 33010602011771号