iOS 原生地图定位,视图中心加大头针,地理位置返编码

2023-11-01 00:08

本文主要是介绍iOS 原生地图定位,视图中心加大头针,地理位置返编码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

info.plist  按需配置
模拟器 地理位置在国外的地理位置返编码没有办法获取

Privacy - Location Always and When In Use Usage Description
Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description

.h

#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGINtypedef void(^BackBlock)(double lat,double lot,NSString *city);@interface MapVC : UIViewController@property(nonatomic,copy)BackBlock backBlock;@endNS_ASSUME_NONNULL_END

.m

#import "MapVC.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface MapVC ()
<
CLLocationManagerDelegate,
MKMapViewDelegate
>
@property(nonatomic,strong)MKMapView *mapView;
/// 定位管理器
@property(nonatomic,strong)CLLocationManager *locationManager;
/// 移动后的位置标记
@property(nonatomic,strong)CLPlacemark *place;/ 放大缩小用
//@property (nonatomic) MKCoordinateRegion region;
@end@implementation MapVC- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.[self.view addSubview:self.mapView];[self.locationManager startUpdatingLocation];
}-(void)viewWillDisappear:(BOOL)animated{[super viewWillDisappear:animated];if (self.backBlock){self.backBlock(self.place.location.coordinate.latitude, self.place.location.coordinate.longitude,self.place.name);}
}#pragma mark ————————— 地图位置更新 —————————————
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{MKCoordinateRegion region;CLLocationCoordinate2D centerCoordinate = mapView.region.center;region.center = centerCoordinate;NSLog(@" 经纬度 %f,%f",centerCoordinate.latitude, centerCoordinate.longitude);CLLocation *location = [[CLLocation alloc]initWithLatitude:centerCoordinate.latitude longitude:centerCoordinate.longitude];[self cityNameFromLoaction:location block:^(CLPlacemark *place, NSString *city) {}];}#pragma mark-CLLocationManagerDelegate
/**
*  更新到位置之后调用
*
*  @param manager   位置管理者
*  @param locations 位置数组
*/
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:( NSArray *)locations
{NSLog(@"定位到了");//停止位置更新[manager stopUpdatingLocation];CLLocation *location = [locations firstObject];//位置更新后的经纬度CLLocationCoordinate2D theCoordinate =  location.coordinate;//设置地图显示的中心及范围MKCoordinateRegion theRegion;theRegion.center = theCoordinate;// 坐标跨度MKCoordinateSpan theSpan;theSpan.latitudeDelta = 0.01;theSpan.longitudeDelta = 0.01;theRegion.span = theSpan;[self.mapView setRegion:theRegion];[self cityNameFromLoaction:location block:^(CLPlacemark *place, NSString *city) {NSLog(@"位置:%@", place.name);}];
}#pragma mark ————————— 地理位置返编码 —————————————
-(void)cityNameFromLoaction:(CLLocation *)location block:(void(^)(CLPlacemark *place ,NSString *city))block
{__weak typeof(self) weakSelf = self;CLGeocoder *geocoder = [[CLGeocoder alloc]init];[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {if (!error){for (CLPlacemark *place in placemarks){weakSelf.place = place;NSString *city = place.name;// 设置地图显示的类型及根据范围进行显示  安放大头针[weakSelf addPointFromCoordinate:place.location.coordinate title:city];block(place ,place.name);}}else{NSLog(@"%@",error);}}];
}#pragma mark ————————— 长按添加大头针事件 —————————————
- ( void )lpgrClick:( UILongPressGestureRecognizer *)lpgr
{// 判断只在长按的起始点下落大头针if (lpgr.state == UIGestureRecognizerStateBegan ){// 首先获取点CGPoint point = [lpgr locationInView:self.mapView];// 将一个点转化为经纬度坐标CLLocationCoordinate2D center = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];[self addPointFromCoordinate:center title:@"位置"];}
}#pragma mark ————————— 添加大头针 —————————————
-(void)addPointFromCoordinate:(CLLocationCoordinate2D)coordinatetitle:(NSString *)title
{NSLog(@"当前城市:%@" ,title);self.title = title;MKPointAnnotation *pinAnnotation = [[MKPointAnnotation alloc] init];pinAnnotation.coordinate = coordinate;pinAnnotation.title = title;[self.mapView removeAnnotations:self.mapView.annotations];[self.mapView addAnnotation:pinAnnotation];
}/***  授权状态发生改变时调用**  @param manager 位置管理者*  @param status  状态*/
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{switch (status) {// 用户还未决定case kCLAuthorizationStatusNotDetermined:{NSLog(@"用户还未决定");break;}// 问受限case kCLAuthorizationStatusRestricted:{NSLog(@"访问受限");break;}// 定位关闭时和对此APP授权为never时调用case kCLAuthorizationStatusDenied:{// 定位是否可用(是否支持定位或者定位是否开启)if([CLLocationManager locationServicesEnabled]){NSLog(@"定位开启,但被拒");}else{NSLog(@"定位关闭,不可用");}break;}// 获取前后台定位授权case kCLAuthorizationStatusAuthorizedAlways:{//  case kCLAuthorizationStatusAuthorized: // 失效,不建议使用NSLog(@"获取前后台定位授权");break;}// 获得前台定位授权case kCLAuthorizationStatusAuthorizedWhenInUse:{NSLog(@"获得前台定位授权");break;}default:break;}
}//获取当前位置
-(CLLocationManager *)locationManager
{if (!_locationManager){CLLocationManager * locationManager = [[CLLocationManager alloc] init];locationManager.delegate = self ;//kCLLocationAccuracyBest:设备使用电池供电时候最高的精度locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;locationManager.distanceFilter = 50.0f;if (([[[ UIDevice currentDevice] systemVersion] doubleValue] >= 8.0)){[locationManager requestAlwaysAuthorization];}_locationManager = locationManager;}return _locationManager;
}-(MKMapView *)mapView
{if (!_mapView){MKMapView *mapView = [[MKMapView alloc]init];// 接受代理mapView.delegate = self;mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];mapView.zoomEnabled = YES ;mapView.showsUserLocation = YES ;mapView.scrollEnabled = YES ;mapView.delegate = self ;// 长按手势  长按添加大头针UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action: @selector (lpgrClick:)];[mapView addGestureRecognizer:lpgr];_mapView = mapView;}return _mapView;
}#pragma mark ————————— 放大事件 —————————————
- (void)addAct
{
//    CLLocationCoordinate2D centCoor = _region.center;
//    MKCoordinateSpan span = _region.span;
//    span.latitudeDelta *= 2;
//    span.longitudeDelta *= 2;
//    MKCoordinateRegion region = MKCoordinateRegionMake(centCoor, span);
//    [self.mapView setRegion:region];
}#pragma mark ————————— 缩小事件—————————————
- (void)minAct
{
//    CLLocationCoordinate2D centCoor = _region.center;
//    MKCoordinateSpan span = _region.span;
//    span.latitudeDelta *= 0.5;
//    span.longitudeDelta *= 0.5;
//    MKCoordinateRegion region = MKCoordinateRegionMake(centCoor, span);
//    [self.mapView setRegion:region];
}-(void)dealloc{NSLog(@"");
}
/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end

调用

#import "MapVC.h"MapVC *vc = [[MapVC alloc]init];__weak typeof(self) weakSelf = self;vc.backBlock = ^(double lat, double lot, NSString * _Nonnull city) {DLog(@"%.15f = %.15f",lat,lot);weakSelf.lot = lot;weakSelf.lat = lat;weakSelf.title = city;};[self.navigationController pushViewController:vc animated:YES];

 

这篇关于iOS 原生地图定位,视图中心加大头针,地理位置返编码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/319061

相关文章

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放

Python动态处理文件编码的完整指南

《Python动态处理文件编码的完整指南》在Python文件处理的高级应用中,我们经常会遇到需要动态处理文件编码的场景,本文将深入探讨Python中动态处理文件编码的技术,有需要的小伙伴可以了解下... 目录引言一、理解python的文件编码体系1.1 Python的IO层次结构1.2 编码问题的常见场景二

Java中字符编码问题的解决方法详解

《Java中字符编码问题的解决方法详解》在日常Java开发中,字符编码问题是一个非常常见却又特别容易踩坑的地方,这篇文章就带你一步一步看清楚字符编码的来龙去脉,并结合可运行的代码,看看如何在Java项... 目录前言背景:为什么会出现编码问题常见场景分析控制台输出乱码文件读写乱码数据库存取乱码解决方案统一使

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

Java 中编码与解码的具体实现方法

《Java中编码与解码的具体实现方法》在Java中,字符编码与解码是处理数据的重要组成部分,正确的编码和解码可以确保字符数据在存储、传输、读取时不会出现乱码,本文将详细介绍Java中字符编码与解码的... 目录Java 中编码与解码的实现详解1. 什么是字符编码与解码?1.1 字符编码(Encoding)1

Python利用GeoPandas打造一个交互式中国地图选择器

《Python利用GeoPandas打造一个交互式中国地图选择器》在数据分析和可视化领域,地图是展示地理信息的强大工具,被将使用Python、wxPython和GeoPandas构建的交互式中国地图行... 目录技术栈概览代码结构分析1. __init__ 方法:初始化与状态管理2. init_ui 方法:

Django中的函数视图和类视图以及路由的定义方式

《Django中的函数视图和类视图以及路由的定义方式》Django视图分函数视图和类视图,前者用函数处理请求,后者继承View类定义方法,路由使用path()、re_path()或url(),通过in... 目录函数视图类视图路由总路由函数视图的路由类视图定义路由总结Django允许接收的请求方法http

SpringBoot3.X 整合 MinIO 存储原生方案

《SpringBoot3.X整合MinIO存储原生方案》本文详细介绍了SpringBoot3.X整合MinIO的原生方案,从环境搭建到核心功能实现,涵盖了文件上传、下载、删除等常用操作,并补充了... 目录SpringBoot3.X整合MinIO存储原生方案:从环境搭建到实战开发一、前言:为什么选择MinI

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.