zoukankan      html  css  js  c++  java
  • iOS 自带地图详解

    1.配置环境:

       1>iOS9为了增强数据访问安全,将所有的http请求都改为了https,为了能够在iOS9中正常使用地图SDK,请在"Info.plist"中进行如下配置,否则影响SDK的使用。

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    

      2>在iOS9中为了能正常调起高德地图App的功能,必须在"Info.plist"中将高德地图App的URL scheme列为白名单,否则无法调起,配置如下:

       <key>

         LSApplicationQueriesSchemes

       </key>

    <array>
        <string>iosamap</string>
    </array>

    2.认识地图:

        MKMapView *mapView =[[MKMapView alloc]init];

        mapView.frame =self.view.bounds;

        // 1.设置地图类型

        mapView.mapType = MKMapTypeStandard;

       // 2.设置跟踪模式(MKUserTrackingModeFollow == 跟踪)

        self.mapView.userTrackingMode = MKUserTrackingModeFollow;这里也是定位功能实现的重要一步

       // 3.设置代理(监控地图的相关行为:比如显示的区域发生了改变)

        self.mapView.delegate = self;

    3. MKMapViewDelegate

    /**

     *  更新到用户的位置时就会调用(显示的位置、显示范围改变) 这里也是定位功能实现的重要一步

     *  userLocation : 大头针模型数据, 对大头针位置的一个封装(这里的userLocation描述的是用来显示用户位置的蓝色大头针)

     */

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

    {

        CLLocationCoordinate2D center = userLocation.location.coordinate;

        MKCoordinateSpan span = MKCoordinateSpanMake(0.2509, 0.2256);

        MKCoordinateRegion region = MKCoordinateRegionMake(center, span);

        [self.mapView setRegion:region animated:YES];//这里是设置地图显示的区域(经纬度,和跨度(经度跨度和纬度跨度))

    一般在这里进行请求周边的数据,添加大头针

    }

     //  地图显示的区域改变了就会调用(显示的位置、显示范围改变)注意:这里一般在做地图拖动的时候显示周边的团购或者东西的时候会用到这个方法

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

     // 地图显示的区域即将改变了就会调用

    - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated

    当用户想返回原来的位置的时候:

    -(void)backToUserLocation {

        CLLocationCoordinate2D center =  self.mapView.userLocation.location.coordinate;

        [self.mapView setCenterCoordinate:center animated:YES];

    }

    4.地理编码和返地理编码 

    @property (nonatomic, strong) CLGeocoder *geocoder;

    - (CLGeocoder *)geocoder

    {

      if (!_geocoder) {

            self.geocoder = [[CLGeocoder alloc] init];

        }

        return _geocoder;

    }

       //  地理编码:地名 -> 经纬度

     - (void)geocode

    {

        // 1.获得输入的地址

        NSString *address = self.addressField.text;

        if (address.length == 0) return;

        // 2.开始编码

        [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

            if (error || placemarks.count == 0)    return;

            // 编码成功(找到了具体的位置信息)

                // 显示最前面的地标信息

                CLPlacemark *firstPlacemark = [placemarks firstObject];//这里是地标(包括经度和纬度还有名字等等信息,这样子就可以进行添加大头针的操作了)

               CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;

                CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;

                self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", latitude];

                self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", longitude];

        }]; 

    }

             //  反地理编码:经纬度 -> 地名

    - (void)reverseGeocode

    {

          // 开始反向编码

        CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];

        [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

            if (error || placemarks.count == 0) return;

                      // 显示最前面的地标信息

                CLPlacemark *firstPlacemark = [placemarks firstObject];

                CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;

                CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;

                self.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude];

                self.longtitudeField.text = [NSString stringWithFormat:@"%.2f", longitude];

        }];

    }

     5.添加大头针(实现MKAnnotation)协议

        也可以自定义大头针

         

     HYAnnotation *anno1 = [[HYAnnotation alloc] init];

     anno1.coordinate = CLLocationCoordinate2DMake(39, 119);

     anno1.title = @"北京";

     anno1.subtitle = @"中国牛逼的地方";

     // 添加一个大头针模型(模型:描述大头针的信息)

    [self.mapView addAnnotation:anno1];

    6.画线(需要开始的位置和重点的位置,可以在进行地理编码时得到)

    - (void)drawLineWithSourceCLPm:(CLPlacemark *)sourceCLPm destinationCLPm:(CLPlacemark *)destinationCLPm

    {

        if (sourceCLPm == nil || destinationCLPm == nil) return;

          // 1.初始化方向请求

        MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

        // 设置起点

        MKPlacemark *sourceMKPm = [[MKPlacemark alloc] initWithPlacemark:sourceCLPm];

        request.source = [[MKMapItem alloc] initWithPlacemark:sourceMKPm];

        self.sourceMKPm = sourceMKPm;

        // 设置终点

        MKPlacemark *destinationMKPm = [[MKPlacemark alloc] initWithPlacemark:destinationCLPm];

        request.destination = [[MKMapItem alloc] initWithPlacemark:destinationMKPm];

        self.destinationMKPm = destinationMKPm;

        // 2.根据请求创建方向

        MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

        // 3.执行请求

        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

            if (error) return;

            for (MKRoute *route in response.routes) {

                // 添加路线遮盖(传递路线的遮盖模型数据)

                [self.mapView addOverlay:route.polyline];

            }

        }];

        // 遮盖 overlay

    }

    - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay

    {

        MKPolylineRenderer *redender = [[MKPolylineRenderer alloc] initWithOverlay:overlay];

        redender.lineWidth = 5;

        redender.strokeColor = [UIColor blueColor];

        return redender;

    }

  • 相关阅读:
    2013级机试D题解析
    关于C# 委托(delegate)与事件(event)的用法及事例
    ASP.NET 简单的柱形图实现(附带示例)
    jQuery 关于IE9上传文件无法进入后台问题的原因及解决办法(ajaxfileupload.js第四弹)
    jQuery 自制上传头像插件-附带Demo实例(ajaxfileupload.js第三弹)
    jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)
    ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload.js第一弹)
    jQuery 表格中实现“删除线”的增进方法
    Skype坑爹报错:“旧版本无法删除,请联络您的技术支持小组 ”的解决办法
    ASP.NET Button控件的UseSubmitBehavior属性引发的血案
  • 原文地址:https://www.cnblogs.com/hongyan1314/p/5802193.html
Copyright © 2011-2022 走看看