需要准备工作按照下图引进类库

需要添加

添加的两个字符串为:NSLocationWhenInUseUsageDescription  /  NSLocationAlwaysUsageDescription

默认定位设置:

设置工作准备完毕上代码:

指示根视图:

    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];self.window.rootViewController = [MapViewController new];

MapViewController.m//设置需要的属性

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import "Mypoint.h"
#import <CoreLocation/CoreLocation.h>
@interface MapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>
@property (nonatomic, strong) MKMapView *mapView;
//经度
@property (nonatomic, strong) UITextField *longitudetext;
//纬度
@property (nonatomic, strong) UITextField *latitudeText;
//经度
@property (nonatomic, strong) UILabel *longitudeLabel;
//纬度
@property (nonatomic, strong) UILabel *latitudelabel;
//防止标注的button[
@property (nonatomic, strong) UIButton *button;
//地址输入
@property (nonatomic, strong) UITextField *destination;
//输入地址查询地图
@property (nonatomic, retain) UIButton *searchButton;
//可以获取设备当前的经纬度信息
@property (nonatomic, strong) CLLocationManager *locManager;
@end@implementation MapViewController

调用:

- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];self.locManager = [[CLLocationManager alloc]init];//代理_locManager.delegate = self;//定位精度_locManager.desiredAccuracy = kCLLocationAccuracyBest;//定位频率,10米定位一次CLLocationDistance distance = 10.0;_locManager.distanceFilter = distance;//更新位置[_locManager requestAlwaysAuthorization];[self.locManager startUpdatingLocation];//查询两个地点之间的距离[self countDistance];//调用布置视图[self configureView];[self setMapView];
}

//布置视图

- (void)configureView{//经度self.longitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 40, 30)];self.longitudeLabel.text = @"经度";self.longitudetext = [[UITextField alloc]initWithFrame:CGRectMake(40, 20, 120, 30)];self.longitudetext.borderStyle = UITextBorderStyleRoundedRect;//纬度self.latitudelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 40, 30)];self.latitudelabel.text = @"纬度";self.latitudeText = [[UITextField alloc]initWithFrame:CGRectMake(40, 50, 120, 30)];self.latitudeText.borderStyle = UITextBorderStyleRoundedRect;//放置标注按钮self.button = [UIButton buttonWithType:(UIButtonTypeSystem)];self.button.frame = CGRectMake(30, 73, 100, 30);[self.button setTitle:@"放置标注" forState:(UIControlStateNormal)];[self.button addTarget:self action:@selector(annotationAction:) forControlEvents:(UIControlEventTouchUpInside)];//地址输入self.destination = [[UITextField alloc]initWithFrame:CGRectMake(200, 26, 100, 30)];self.destination.borderStyle = UITextBorderStyleRoundedRect;//查询按钮self.searchButton = [UIButton buttonWithType:(UIButtonTypeSystem)];self.searchButton.frame = CGRectMake(200, 46, 100, 30);[self.searchButton setTitle:@"查询" forState:(UIControlStateNormal)];[self.searchButton addTarget:self action:@selector(detailSearchAction:) forControlEvents:(UIControlEventTouchUpInside)];[self.view addSubview:self.button];[self.view addSubview:self.latitudelabel];[self.view addSubview:self.longitudeLabel];[self.view addSubview:self.longitudetext];[self.view addSubview:self.latitudeText];[self.view addSubview:self.searchButton];[self.view addSubview:self.destination];
}
- (void)countDistance{CLLocation *loc1 = [[CLLocation alloc]initWithLatitude:34 longitude:113];CLLocation *loc2 = [[CLLocation alloc]initWithLatitude:35 longitude:113];CLLocationDistance distance = [loc1 distanceFromLocation:loc2];NSLog(@"(%@) 和 (%@)的距离为: %f", loc1, loc2, distance);
}

#pragma mark - CLLocationManagerDelegate Methods

// 此方法会被频繁调用

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//    NSLog(@"didUpdateLocations---%lu", (unsigned long)locations.count);// 用来表示某个位置的地理信息, 比如经纬度, 海拔等等CLLocation *location = locations.lastObject;// location.coordinate.latitude  维度// location.coordinate.longitude 经度NSLog(@"经度 == %f, 维度 == %f", location.coordinate.longitude, location.coordinate.latitude);self.longitudetext.text = [NSString stringWithFormat:@"%f",location.coordinate.longitude];self.latitudeText.text = [NSString stringWithFormat:@"%f",location.coordinate.latitude];// 停止更新位置(不用定位服务时马上停止, 因为非常耗电)
//    [manager stopUpdatingLocation];
}

//调出地图

- (void)setMapView{//创建地图视图,初始化参数//MKMapTypeStandard 显示街道和道路//MKMapTypeSatellite 显示卫星//MKMapTypeHybrid 显示混合地图self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 100, 320, 460)];[self.mapView setMapType:MKMapTypeStandard];//显示用户当前的坐标,打开地图有相应的提示self.mapView.showsUserLocation = YES;//设置地图代理self.mapView.delegate = self;[self.view addSubview:self.mapView];
}

#pragma mark 根据输入的经纬度确定位置

//放置标注

//放置标注
- (void)annotationAction:(UIButton *)sender{//创建CLLocation 设置经纬度CLLocationCoordinate2D location = CLLocationCoordinate2DMake([[self.latitudeText text]floatValue], [[self.longitudetext text]floatValue] );//创建标题NSString *title = [NSString stringWithFormat:@"%f,%f",location.latitude,location.longitude];Mypoint *myPoint = [[Mypoint alloc]initWithCoordinate:location andTitle:title];//添加标注[self.mapView addAnnotation:myPoint];//放大到标注的位置MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.01, 0.01);[self.mapView setRegion:region];[self showLocation];
}

//根据输入的经纬度显示位置

//根据输入的经纬度显示位置
- (void)showLocation{//创建CLLocation 设置经纬度CLLocationCoordinate2D location = CLLocationCoordinate2DMake([[self.latitudeText text]floatValue], [[self.longitudetext text]floatValue] );//放大到标注的位置MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.01, 0.01);[self.mapView setRegion:region animated:YES];
}

#pragma mark 根据输入的地址搜寻位置

//根据地址输入搜索地图

//根据地址输入搜索地图
- (void)detailSearchAction:(UIButton *)search{if (_destination.text == nil || [_destination.text length] == 0) {return;}CLGeocoder *geocode = [[CLGeocoder alloc]init];[geocode geocodeAddressString:_destination.text completionHandler:^(NSArray *placemarks, NSError *error) {if (error || placemarks.count == 0) {NSLog(@"地址不存在");}else{for (CLPlacemark *placemark in placemarks) {NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode);}CLPlacemark *firstPlacemark = [placemarks firstObject];CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;//显示经纬度self.latitudeText.text = [NSString stringWithFormat:@"%.2f",latitude];self.longitudetext.text = [NSString stringWithFormat:@"%.2f",longitude];[self showLocation];[self searchDetailLocationAction];}}];
}

//根据地址搜寻位置

//根据地址搜寻位置
- (void)searchDetailLocationAction{//创建CLLocation 设置经纬度CLLocationCoordinate2D location = CLLocationCoordinate2DMake([self.latitudeText.text floatValue], [self.longitudetext.text floatValue]);//创建标题NSString *title = [NSString stringWithFormat:@"%f,%f",[self.latitudeText.text floatValue], [self.longitudetext.text floatValue]];Mypoint *myPoint = [[Mypoint alloc]initWithCoordinate:location andTitle:title];//添加标注[self.mapView addAnnotation:myPoint];//放大到标注的位置MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.01, 0.01);[self.mapView setRegion:region];
}

建一个类:

//.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Mypoint : NSObject<MKAnnotation>
//实现MKAnnotion协议必须要定义这个属性
@property (nonatomic,readonly)CLLocationCoordinate2D coordinate;
//标题
@property (nonatomic,copy)NSString *title;//初始化方法
- (id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t;@end
//.m
#import "Mypoint.h"@implementation Mypoint
//初始化方法
- (id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t{if (self = [super init]) {_coordinate = c;_title = t;}return self;
}
@end

最终效果:

关注博主微博每日更新技术:http://weibo.com/hanjunqiang

iOS中 百度地图详解 韩俊强的博文相关推荐

  1. iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客

    版权声明:本文为博主原创文章,未经博主允许不得转载. 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 简单介绍: [objc] view plaincopy //  ...

  2. iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客

    原文地址:http://blog.csdn.net/qq_31810357/article/details/51111702 前言:最近研究了一下语音识别,从百度语音识别到讯飞语音识别:首先说一下个人 ...

  3. iOS中 本地通知/本地通知详解 韩俊强的博客

    布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 Notification是智能手机应用编 ...

  4. iOS中 最新微信支付/最全的微信支付教程详解 韩俊强的博客

    亲们, 首先让我们来看一下微信支付的流程吧. 1. 注册微信开放平台,创建应用获取appid,appSecret,申请支付功能,申请成功之后会返回一些参数. 2. 下载微信支付sdk 3. 客户端请求 ...

  5. iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客

    第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graphics 和Quartz 2D的区别 quartz是一个通用的术语,用于描述在IOS和 ...

  6. [置顶] iOS中 支付宝钱包详解/第三方支付

    [置顶] iOS中 支付宝钱包详解/第三方支付 韩俊强的博客 每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 一.在app中成功完成支付宝支付的过程 1.申请支付 ...

  7. iOS中ImageIO框架详解与应用分析

    2019独角兽企业重金招聘Python工程师标准>>> iOS中ImageIO框架详解与应用分析 一.引言 ImageIO框架提供了读取与写入图片数据的基本方法,使用它可以直接获取到 ...

  8. react引入百度地图详解(配置智能检索反向地址解析获取实际地标)

    获取百度地图密钥 获取密钥流程(网址:https://blog.csdn.net/chenyueliu/article/details/106055426) 在index.html中引入 在***** ...

  9. iOS中 支付宝钱包详解/第三方支付 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包.参考网址 ...

最新文章

  1. 用python实现自动填数生成表格v2.0
  2. 你想带一顶什么样的硕士帽(转载)
  3. Little Sub and Applese
  4. Linux buff/cache和清理占用过高
  5. java商品管理系统_【Java Web】简易商品信息管理系统——首个Web项目
  6. 机器学习集成模型学习——Bagging集成学习(三)
  7. angular自带的一些api_Angular2.0正式版api使用漫谈
  8. 每日一题_JavaScript.两种方式实现网页加载后onload绑定多个函数?
  9. fragment--总结
  10. 用计算机函数,信息技术应用 用计算机画函数图象教案设计(一等奖)
  11. 四种依恋类型_你们之间的爱情是哪种类型 | 爱情依恋关系测评
  12. 基于STM32电压检测和电流检测
  13. GitHub中的神奇开源,汇聚天涯神贴讨论房价涨跌,买房好帮手!
  14. java-net-php-python-java门诊信息管理系统计算机毕业设计程序
  15. 给 App 提速:Android 性能优化总结
  16. js将base64图片处理成背景透明png
  17. 支付宝固码php模拟,【推荐】PC_支付宝收款助手【支持支付宝固码+自动生成】...
  18. 前端学习day38day39day40:09-JS基础之各类型常用api
  19. jenkins + jmeter +ant 发送邮件失败
  20. 多目标优化(一)简单的 NSGA-Ⅱ

热门文章

  1. dnf钓鱼网站源码php,DNF钓鱼网站详细解密 千万不要贪小便宜
  2. Vue3+TypeScript从入门到精通系列之:Try changing the lib compiler option to es2015 or later
  3. Python实现统计文本中各单词数量
  4. Esxi6.7-7.0设置自动启动无效原因
  5. 深度学习前沿算法思想
  6. mac M1安装Matlab R2020a
  7. 关于电脑WLAN突然消失
  8. linux服务器开机管理,Linux服务器开机自动启动服务或脚本的方法
  9. java导出excel,导入excel,导出csv工具类整理
  10. 基于TensorFlow的歌曲曲风变换