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

需要添加

添加的两个字符串为: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中 断点下载详解 韩俊强的博客

    布局如下: 基本拖拉属性: #import "ViewController.h" #import "AFNetworking.h" @interface Vie ...

  2. iOS中 扫描二维码/生成二维码详解 韩俊强的博客

    最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

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

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...

  4. iOS中 最新支付宝支付(AliPay) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 现在的支付方式一般有三种, 支付宝, 微信, 网银. 个人觉得最简单易用的还是支付宝, 微信虽然看起来币支付宝要简单 ...

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

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

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

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...

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

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 亲们, 首先让我们来看一下微信支付的流程吧. 1. 注册微信开放平台,创建应用获取appid,appSecret, ...

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

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 原文地址:http://blog.csdn.net/qq_31810357/article/details/5111 ...

  9. iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 整体布局如下:     程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang  ...

随机推荐

  1. ignorable tips

    枚举 索引从0开始 sort 默认升序排列  Array.Sort(intSort); //复制数组 Array.Copy(intSort,intNew,3); intsort 源数组 intnew ...

  2. vue2.0+ 从插件开发到npm发布

    vue: V2.5.11 此篇尽量详细,清楚的讲解vue插件的开发到npm的发布,想想将你自己做的东西展示给广大"网民",心里还是有点小激动的...-^_^ 先上一下插件效果图-- ...

  3. Oracle中时间和日期函数总结

    查看当前日期格式:select * from nls_session_parameters where parameter='NLS_DATE_FORMAT'; 修改日期的格式: alter sess ...

  4. 用go实现常用算法与数据结构——队列(queue)

    queue 简介 队列是一种非常常见的数据结构,日常生活中也能经常看到.一个典型的队列如下图(图片来自 segmentfault): 可以看出队列和我们日常生活中排队是基本一致的.都遵循 FIFO(F ...

  5. MySQL 连接的使用

    MySQL 连接的使用 在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JOIN ...

  6. Android 学习笔记一 自定义按钮背景图

    入门学到的一些组件都是比较规矩的,但在实际应用中,我们需要更多特色的组件,例如一个简单的Button,所以我们必须要自定义它的属性. 遇到的问题:用两张图片来代替按钮,分别表示点击前后 解决方法:用I ...

  7. linux下安装apache(httpd-2.4.3版本)各种坑

    博主的linux是ubuntu 14.04.3. 在安装apache最新版httpd-2.4.3的时候遇到各种坑. 先提供安装apache httpd-2.4.3所需要的包,博主已经整理好,下载地址: ...

  8. SpringMVC+BUI实现文件上传(附详解,源码下载)

    中午有限时间写这博文,前言就不必多说了,直奔主题吧. BUI是一个前端框架,关于BUI的介绍请看博主的文章那些年用过的一些前端框架. 下面我们开始实例的讲解! 一.效果演示: 上传成功后,会发现本地相 ...

  9. Dynamics CRM2016 时间字段属性中的新增行为

    之前的博客中有特地介绍过CRM中的时间字段以及它在不同的应用场景中涉及的时制转换,而CRM2016又给时间字段添加了新的行为,具体见下属截图,简单介绍下每个图中对应的行为的意思,最后会做demo来具体 ...

  10. activiti processEngineLifecycleListener使用

    1.1.1. 前言 实际开发中,有需求如下: 第一:项目启动部署的时候,我们需要监控activiti 工作流引擎是否真正的已经实例化启动了,这里说的是工作流引擎的启动,不是流程实例的启动,对此要特别说 ...