美文网首页
iOS地图和定位

iOS地图和定位

作者: 靠北的北 | 来源:发表于2017-06-06 16:58 被阅读131次

iOS地图定位 本文发布在http://he8090.cn/2016/07/18/地图与定位/

  • 导入地图框架
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

1、定位

//CLLocationManager定位器
//1 判断设备是否支持定位服务
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"不支持此设备");
        return;
    }
    //2 创建定位服务
    _locationManager = [[CLLocationManager alloc] init];
    //3 请求用户授权(使用期间)  在info.plist文件添加的字段必须与设置的字段一致
    [_locationManager requestWhenInUseAuthorization];

    //4 配置定位服务
    //4.1 设置定位精度
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //4.2 设置过滤距离(以用户位置方圆100米为半径,实际位置超过该范围才会更新位置。否则不更新)
    _locationManager.distanceFilter = 100;
    //4.3 设置代理
    _locationManager.delegate = self;

    //5 开始定位,定位为耗时操作。定位成功后一定关闭定位。
    [_locationManager startUpdatingLocation];
// 在模拟器上定位失败的解决方法
//1 重置模拟器
//2 模拟器设置。debug - location - apple 和 customer 之间切换

2、地图标点

//字符转化
    CGFloat latitude = [dic[@"LATITUDE"] floatValue];
    CGFloat longtitude = [dic[@"LONGITUDE"] floatValue];

    //设置地图显示的中心点及缩放比例
    CLLocationCoordinate2D center = {latitude,longtitude};
    MKCoordinateSpan span;

    span.latitudeDelta = 1;
    span.longitudeDelta = 1;

    MKCoordinateRegion region = {center,span};

    [self.mapView setRegion:region animated:YES];


        MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
        anno.title = dic[@"NAME"];
        anno.subtitle = dic[@"ADDRESS"];
        anno.coordinate = center;

        //调用此方法在地图上加坐标点
        [self.mapView addAnnotation:anno];

3、根据地名获取经纬度

- (void)showLatitudeFromCityName:(NSString *)cityName {

       __block  NSDictionary *dic = [NSDictionary dictionary];

    CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
    [myGeocoder geocodeAddressString:cityName completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count] > 0 && error == nil)
        {
            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];


            dic = @{@"LONGITUDE":@(firstPlacemark.location.coordinate.longitude),
                    @"LATITUDE":@(firstPlacemark.location.coordinate.latitude)
                    };

            [self showPointOfLatitude:dic];
        }
        else if ([placemarks count] == 0 && error == nil)
        {
            NSLog(@"Found no placemarks.");
        }
        else if (error != nil)
        {
            NSLog(@"An error occurred = %@", error);
        }
    }];
}

4、反向解析

(1)与长按手势进行配合(手势添加到地图上)
//长按手势
    UILongPressGestureRecognizer *gest = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

    [self.mapView addGestureRecognizer:gest];

(2)根据选中的点,获取相关点
- (void)longPressAction:(UILongPressGestureRecognizer *)sender{

    //获取触摸的点
    CGPoint point = [sender locationInView:self.mapView];
    //通过触摸的点获取经纬度
    CLLocationCoordinate2D coord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    //组装参数
    CLLocation *location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
    //获取信息
    [geo reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //获取位置
        if (placemarks.count != 0) {
            CLPlacemark *placeMark = placemarks.firstObject;

            NSArray *addressArray = placeMark.addressDictionary[@"FormattedAddressLines"];

            //拼接地址
            NSMutableString *address = [[NSMutableString alloc] init];

            for (int i = 0; i < addressArray.count; i ++) {
                [address appendString:addressArray[i]];
            }

            MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
            anno.title = placeMark.name;
            anno.subtitle = address;
            anno.coordinate = coord;

            [self.mapView addAnnotation:anno];

        }

    }];
}

相关文章

  • IOS地图定位导航

    title : IOS地图定位导航category : UI 地图定位导航 标签(空格分隔): IOS 概述 I...

  • 定位和地图

    为了使用iOS中的地图和定位功能,我们需要使用Core Location和Map Kit,分别用于地理定位和地图展...

  • iOS定位和地图

    一.定位 1.iOS8以后前台定位 A.代码 B.配置信息Info.plist 2.iOS8以后后台定位 A.代码...

  • IOS 定位和地图

    定位 //第一步导入框架 @import CoreLocation; //协议和属性 @interface Loc...

  • IOS定位和地图

    现在手机软件基本上都需要定位,比如打个的,附近的人,附近的餐厅等等,这些应用都需要定位和地图服务。 我这个人分不清...

  • iOS地图和定位

    iOS地图定位 本文发布在http://he8090.cn/2016/07/18/地图与定位/ 导入地图框架 1、...

  • iOS 定位和地图

    简介: 在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆、找KTV、找电影院等等 导航:根据...

  • 地图与定位

    OCiOS开发:地图与定位 - 李鴻耀 - 博客频道 - CSDN.NET iOS开发之地图-定位/...

  • 定位及耗电

    iOS-GPS定位基础知识 iOS -GPS定位服务和地图应用是两套完全不同的API iOS7 的四种定位服务-G...

  • iOS Mapkit的使用

    【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等 标签:iOS地图mapkit 1.显示地图 (1)首...

网友评论

      本文标题:iOS地图和定位

      本文链接:https://www.haomeiwen.com/subject/zmqnfxtx.html