与地图相关的代码段

    //获取当前位置

    MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];

    //当前经纬度

    float currentLatitude = mylocation.placemark.location.coordinate.latitude;
    float currentLongitude = mylocation.placemark.location.coordinate.longitude;

    //设定为开始坐标

    CLLocationCoordinate2D startCoor = CLLocationCoordinate2DMake(currentLatitude, currentLongitude);

    //目的地位置

    

    CLLocationCoordinate2D endCoor2D;

     //中欧国际工商学院

    endCoor2D.latitude = 31.232887369792;
    endCoor2D.longitude = 121.6053765191;


    //目的地坐标

    CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(endCoor2D.latitude, endCoor2D.longitude);

    //打开地图,先判断设备上有没有安装百度地图,如果有就打开百度地图,没有就打开系统自带地图

NSString *toName = @"中欧国际工商学院";
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
        NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",
                               startCoor.latitude, startCoor.longitude, endCoor.latitude, endCoor.longitude, toName];
        
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
    }else{
    //打开系统自带地图
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
    [MKMapItem openMapsWithItems:@[mylocation, toName]
                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
    }

//根据地点获取经纬度

    NSString *oreillyAddress = @"中欧国际工商学院";
    CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
    [myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count] > 0 && error == nil) {
            NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
            NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
            NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);
        }
        else if ([placemarks count] == 0 && error == nil) {
            NSLog(@"Found no placemarks.");
        } else if (error != nil) {
            NSLog(@"An error occurred = %@", error);
        }  
    }];

//    根据经纬度获取地点

    CLLocationDegrees latitude = 31.237094;
    CLLocationDegrees longitude = 121.596424;
    CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];

    CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
    [clGeoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray *placemarks,NSError *error) {
        for (CLPlacemark *placeMark in placemarks)
        {
            NSDictionary *addressDic = placeMark.addressDictionary;
            
            NSString *city = [addressDic objectForKey:@"City"];
            NSString *country = [addressDic objectForKey:@"Country"];
            NSString *name = [addressDic objectForKey:@"Name"];
            NSString *state = [addressDic objectForKey:@"State"];
            NSString *street = [addressDic objectForKey:@"Street"];
            NSString *subLocality = [addressDic objectForKey:@"SubLocality"];
            NSString *subThoroughfare = [addressDic objectForKey:@"SubThoroughfare"];
            NSString *thoroughfare = [addressDic objectForKey:@"Thoroughfare"];
            NSArray *arr = [addressDic objectForKey:@"FormattedAddressLines"];
            
            NSLog(@"city:%@",city);
            NSLog(@"country:%@",country);
            NSLog(@"name:%@",name);
            NSLog(@"state:%@",state);
            NSLog(@"street:%@",street);
            NSLog(@"subLocality:%@",subLocality);
            NSLog(@"subThoroughfare:%@",subThoroughfare);
            NSLog(@"thoroughfare:%@",thoroughfare);
            NSLog(@"FormattedAddressLines:%@",[arr firstObject]);
        }
    }];


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值