iOS开发调用苹果自带的地图应用

iOS6.0以后我们可以在应用中直接调用苹果自带的地图应用了,并且可以实现搜索单个位置,查询线路。
难度不大,但是搜索的时候只能传过去经纬度坐标,因此很多时候就要把地理位置信息转换成经纬度坐标了,具体看代码:


    NSString *oreillyAddress =@"北京市东城区东单";

    //下边就是利用CLGeocoder把地理位置信息转换成经纬度坐标;
 
    CLGeocoder *myGeocoder = [[CLGeocoderalloc]init];
    [myGeocodergeocodeAddressString:oreillyAddresscompletionHandler:^(NSArray*placemarks,NSError *error) {
        
        //placemarks就是转换成坐标的数组(当地理位置信息不够准确的时候可能会查询出来几个坐标);
       if ([placemarkscount] >0 && error ==nil){
            
           NSLog(@"%lu ", (long)[placemarkscount]);
           NSMutableArray *arrtemp=[[NSMutableArrayalloc]initWithCapacity:0];
            
           for (int i=0; i<[placemarkscount]; i++) {
               CLPlacemark *firstPlacemark = [placemarksobjectAtIndex:i];
                //查看CLPlacemark这个类可以看到里边有很多属性;位置的名称等。
                
                //下面就是来调用苹果地图应用了;
               MKMapItem *toLocation = [[MKMapItemalloc]initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:CLLocationCoordinate2DMake(firstPlacemark.location.coordinate.latitude,firstPlacemark.location.coordinate.longitude)addressDictionary:nil]];
                toLocation.name=firstPlacemark.name;

                [arrtempaddObject:toLocation];
            }
           //打开地图
           //1.搜索位置;
            //[MKMapItem openMapsWithItems:arrtemp launchOptions:nil];
           //2.查询线路;
           MKMapItem *currentLocation = [MKMapItemmapItemForCurrentLocation];//当前位置
            NSMutableDictionary *dict=[[NSMutableDictionaryalloc]initWithCapacity:0];
            [dict setObject:MKLaunchOptionsDirectionsModeWalkingforKey:MKLaunchOptionsDirectionsModeKey];
            [dict setObject:[NSNumbernumberWithBool:YES]forKey:MKLaunchOptionsShowsTrafficKey];

            [MKMapItemopenMapsWithItems:[NSArrayarrayWithObjects:currentLocation, [arrtemp objectAtIndex:0],nil]launchOptions:dict];
            //MKLaunchOptionsDirectionsModeKey方式:步行,开车
            //MKLaunchOptionsShowsTrafficKey  显示交通状况
            //MKMapItem 进去后看其他属性
        }
       elseif ([placemarkscount] ==0 &&
                 error ==nil){
//            NSLog(@"Found no placemarks.");
        }
       elseif (error !=nil){
//            NSLog(@"An error occurred = %@", error);
        }
    }];

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值