地图实现地点查找和导航

环境:系统版本:

OSX 10.10.2

Xcodel版本:7.1.1

功能:用自带地图实现查找,导航


1.首先需要在info.plist中添加NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription两个属性


导入CoreLocation.framework框架


2.创建CLLocationManager类,启动定位功能

// 定位管理
    self.locationManager = [[CLLocationManager alloc]init];
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务当前未打开");
        return;
    }
    // 请求用户授权
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    }else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){
        // 设置代理
        self.locationManager.delegate = self;
        // 设置定位精度
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 定位频率,每隔多少米定位一次
        CLLocationDistance distance = 10.0; // 十米定位一次
        self.locationManager.distanceFilter = distance;
        // 启动跟踪定位
        [self.locationManager startUpdatingLocation];
    }

遵守CLLocationManagerDelegate代理,实现跟踪定位

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: 'Heiti SC Light';"><span style="line-height: normal; font-family: Menlo;">// </span>跟踪定位代理</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    CLLocation *location = [locations firstObject]; // <span style="line-height: normal; font-family: 'Heiti SC Light';">取出第一个位置</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    CLLocationCoordinate2D coordinate = location.coordinate; // <span style="line-height: normal; font-family: 'Heiti SC Light';">位置坐标</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">     NSLog(@"<span style="line-height: normal; font-family: 'Heiti SC Light';">经度:</span>%f,<span style="line-height: normal; font-family: 'Heiti SC Light';">纬度:</span>%f,<span style="line-height: normal; font-family: 'Heiti SC Light';">海拔:</span>%f,<span style="line-height: normal; font-family: 'Heiti SC Light';">航向:</span>%f,<span style="line-height: normal; font-family: 'Heiti SC Light';">行走速度:</span>%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    _jinddu = coordinate.longitude;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    _weidu = coordinate.latitude;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: 'Heiti SC Light';"><span style="line-height: normal; font-family: Menlo;">    </span><span style="line-height: normal; font-family: Menlo;">//</span>如果不需要实时定位,使用完即使关闭定位服务</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    [_locationManager stopUpdatingLocation];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p>

2.根据坐标取得地名

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: 'Heiti SC Light';"><span style="line-height: normal; font-family: Menlo;">// </span>根据坐标取得地名</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    //<span style="line-height: normal; font-family: 'Heiti SC Light';">反地理编码</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        CLPlacemark *placemark=[placemarks firstObject];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        NSLog(@"<span style="line-height: normal; font-family: 'Heiti SC Light';">详细信息</span>:%@",placemark.addressDictionary);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    }];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p>

3.根据地名确定地理坐标

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(void)getCoordinateByAddress:(NSString *)address{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    //<span style="line-height: normal; font-family: 'Heiti SC Light';">地理编码</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: 'Heiti SC Light';"><span style="line-height: normal; font-family: Menlo;">        </span><span style="line-height: normal; font-family: Menlo;">//</span>取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        CLPlacemark *placemark=[placemarks firstObject];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;">        </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        CLLocation *location=placemark.location;//<span style="line-height: normal; font-family: 'Heiti SC Light';">位置</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        CLRegion *region=placemark.region;//<span style="line-height: normal; font-family: 'Heiti SC Light';">区域</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        NSDictionary *addressDic= placemark.addressDictionary;//<span style="line-height: normal; font-family: 'Heiti SC Light';">详细地址信息字典</span>,<span style="line-height: normal; font-family: 'Heiti SC Light';">包含以下部分信息</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *name=placemark.name;//<span style="line-height: normal; font-family: 'Heiti SC Light';">地名</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *thoroughfare=placemark.thoroughfare;//<span style="line-height: normal; font-family: 'Heiti SC Light';">街道</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *subThoroughfare=placemark.subThoroughfare; //<span style="line-height: normal; font-family: 'Heiti SC Light';">街道相关信息,例如门牌等</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *locality=placemark.locality; // <span style="line-height: normal; font-family: 'Heiti SC Light';">城市</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *subLocality=placemark.subLocality; // <span style="line-height: normal; font-family: 'Heiti SC Light';">城市相关信息,例如标志性建筑</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *administrativeArea=placemark.administrativeArea; // <span style="line-height: normal; font-family: 'Heiti SC Light';">州</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *subAdministrativeArea=placemark.subAdministrativeArea; //<span style="line-height: normal; font-family: 'Heiti SC Light';">其他行政区域信息</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *postalCode=placemark.postalCode; //<span style="line-height: normal; font-family: 'Heiti SC Light';">邮编</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *ISOcountryCode=placemark.ISOcountryCode; //<span style="line-height: normal; font-family: 'Heiti SC Light';">国家编码</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *country=placemark.country; //<span style="line-height: normal; font-family: 'Heiti SC Light';">国家</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *inlandWater=placemark.inlandWater; //<span style="line-height: normal; font-family: 'Heiti SC Light';">水源、湖泊</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *ocean=placemark.ocean; // <span style="line-height: normal; font-family: 'Heiti SC Light';">海洋</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSArray *areasOfInterest=placemark.areasOfInterest; //<span style="line-height: normal; font-family: 'Heiti SC Light';">关联的或利益相关的地标</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        NSLog(@"<span style="line-height: normal; font-family: 'Heiti SC Light';">位置</span>:%@,<span style="line-height: normal; font-family: 'Heiti SC Light';">区域</span>:%@,<span style="line-height: normal; font-family: 'Heiti SC Light';">详细信息</span>:%@",location,region,addressDic);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    }];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p>
   

使用苹果自带地图

1.根据单个地名在地图上定位

-(void)location{
    //地理编码
    [_geocoder geocodeAddressString:@"上海市" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *clPlacemark=[placemarks firstObject];//获取第一个地标
        MKPlacemark *mkplacemark=[[MKPlacemark alloc]initWithPlacemark:clPlacemark];//定位地标转化为地图的地标
        NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
        MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:mkplacemark];
        [mapItem openInMapsWithLaunchOptions:options];
    }];
}

2.定位多个地名

- (void)listPlacemark
{
    //根据“北京市”进行地理编码
    [_geocoder geocodeAddressString:@"北京市" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *clPlacemark1=[placemarks firstObject];//获取第一个地标
        MKPlacemark *mkPlacemark1=[[MKPlacemark alloc]initWithPlacemark:clPlacemark1];
        
        //注意地理编码一次只能定位到一个位置,不能同时定位,所在放到第一个位置定位完成回调函数中再次定位
        [_geocoder geocodeAddressString:@"郑州市" completionHandler:^(NSArray *placemarks, NSError *error) {
            
            CLPlacemark *clPlacemark2=[placemarks firstObject];//获取第一个地标
            MKPlacemark *mkPlacemark2=[[MKPlacemark alloc]initWithPlacemark:clPlacemark2];
            NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
            //MKMapItem *mapItem1=[MKMapItem mapItemForCurrentLocation];//当前位置
            MKMapItem *mapItem1=[[MKMapItem alloc]initWithPlacemark:mkPlacemark1];
            MKMapItem *mapItem2=[[MKMapItem alloc]initWithPlacemark:mkPlacemark2];
            [MKMapItem openMapsWithItems:@[mapItem1,mapItem2] launchOptions:options];
        }];
    }];
}

3.实现导航功能

-(void)turnByTurn{
    //根据“北京市”地理编码
    [_geocoder geocodeAddressString:@"北京市" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *clPlacemark1=[placemarks firstObject];//获取第一个地标
        MKPlacemark *mkPlacemark1=[[MKPlacemark alloc]initWithPlacemark:clPlacemark1];
        //注意地理编码一次只能定位到一个位置,不能同时定位,所在放到第一个位置定位完成回调函数中再次定位
        [_geocoder geocodeAddressString:@"郑州市" completionHandler:^(NSArray *placemarks, NSError *error) {
            CLPlacemark *clPlacemark2=[placemarks firstObject];//获取第一个地标
            MKPlacemark *mkPlacemark2=[[MKPlacemark alloc]initWithPlacemark:clPlacemark2];
            NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
            //MKMapItem *mapItem1=[MKMapItem mapItemForCurrentLocation];//当前位置
            MKMapItem *mapItem1=[[MKMapItem alloc]initWithPlacemark:mkPlacemark1];
            MKMapItem *mapItem2=[[MKMapItem alloc]initWithPlacemark:mkPlacemark2];
            [MKMapItem openMapsWithItems:@[mapItem1,mapItem2] launchOptions:options];
        }];
    }];
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值