天天看点

iOS 地图的简单实用iOS 地图的简单实用

iOS 地图的简单实用

简单介绍 iOS 地图的简单使用:

- plist.info文件的配置

- CLLocationManager的使用

- 地理编译与反地理编译

- MapKit 的使用

plist.info文件的配置

<key>UIBackgroundModes</key>
    <array>
        <string>location</string>
    </array>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>开启定位啦设置什么就会提示什么</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>NSLocationAlwaysUsageDescription</string>
           

CLLocationManager的使用

//实例化CLLocationManager  位置管理器
 CLLocationManager *locationManager = [[CLLocationManager alloc]init];
 //在使用时才授权   
 //一直授权 [self.locationManager requestAlwaysAuthorization]
 [self.locationManager requestWhenInUseAuthorization];
//设置代理对象
 locationManager.delegate = self;
//判断版本,开始定位
 if ([UIDevice currentDevice].systemVersion.floatValue >= ) {
        locationManager.allowsBackgroundLocationUpdates = YES;
    }
    [locationManager startUpdatingLocation]; 
           

locationManager的代理方法,只有定位是才会被调用,由于定位调用的很频繁,所以获取位置后先将他暂停掉

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    NSLog(@"%@",locations);
    [manager stopUpdatingLocation];
}
           

在使用CLLocationManager需要导入CoreLocation/CoreLocation.h类

地理编译与反地理编译

CLGeocoder地理编译与反地理编译用到,可以说只要用到坐标的转换就得使用 CLGeocoder类

注意 -先是纬度,后是经度

我们日常所说的经纬度 将经度放前,纬度放后.但是在程序中的经纬度顺序是相反的 ,先是纬度,后是经度

地理编译

地理编译就是输入地理名字,然后转换成经纬度的坐标

CLGeocoder *geocoder = [[CLGeocoder alloc]init];
 [geocoder geocodeAddressString:@"北京" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        if (error) {
            NSLog(@"%@",error);
        }
        for (CLPlacemark *pm in placemarks) {
            NSLog(@"%@",pm.name);
        }
        CLPlacemark *placemark = [placemarks firstObject];  
              NSLog(@"%f",placemark.location.coordinate.longitude);
NSLog(@"%f",placemark.location.coordinate.longitude);
        NSLog(@"%@",placemark.locality); 
    }];
           

反地理编译

反地理编译就是把经纬度转换成地名

CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    CLLocation *location = [[CLLocation alloc]initWithLatitude: longitude:];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        for (CLPlacemark *placemark in placemarks) {
            NSLog(@"%@",placemark.name);
        }
    }];
           

MapKit 的使用

MapKit 与常用的 UI控件相对独立,需要手动在工程中添加包,之后导入头文件 MapKit/MapKit.h

iOS 地图的简单实用iOS 地图的简单实用

iOS9新特性 显示实时交通状况

self.mapKit.showsTraffic = YES;
           

设置地图模式

/*
     MKMapTypeStandard   标准地图
     MKMapTypeSatellite  卫星地图
     MKMapTypeHybrid     鸟瞰地图
     */
self.mapKit.mapType = MKMapTypeStandard;
           

用户跟踪模式

self.mapKit.userTrackingMode = MKUserTrackingModeFollow;
           

设置代理

self.mapKit.delegate = self;
           

实现代理方法

mapView的代理方法 定位到用户当前位置时调用

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
       [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error) {
            NSLog(@"出错啦 %@",error);
            return ;
        }
        CLPlacemark *placemark = [placemarks firstObject];
           NSLog(@"name,%@",placemark.name);//位置名字
           NSLog(@"thoroughfare,%@",placemark.thoroughfare);//街道名字
           NSLog(@"subThtoughfare,%@",placemark.subThoroughfare);//子街道
           NSLog(@"locality,%@",placemark.locality);               // 市
           NSLog(@"subLocality,%@",placemark.subLocality);         // 区
           NSLog(@"country,%@",placemark.country);                 // 国家      
    }];
}
           

mapView的代理方法 大小改变时调用

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    NSLog(@"当被改变时候调用");
     NSLog(@"latitudeDelta: %f   longitudeDelta:  %f",self.mapKit.region.span.latitudeDelta,self.mapKit.region.span.longitudeDelta);
}
           

改变 mapView 的大小

double longtidude = self.mapKit.region.span.longitudeDelta*;
    double latitudeDelta = self.mapKit.region.span.latitudeDelta*;

    MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longtidude);

    [self.mapKit setRegion:MKCoordinateRegionMake(self.mapKit.centerCoordinate, span) animated:YES];