ios 通过经纬度定位城市,通过城市名定位经纬度

通过经纬度定位城市:

CLGeocoder * geoCoder = [[[CLGeocoder allocinitautorelease];

    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks,NSError *error) {

        for (CLPlacemark * placemark in placemarks) {

            NSString *myCity = placemark.locality;

            if ([myCity isEqualToString:@"长安"]) {

                myCity = @"西安";

            }

            self.myLabel.text = [NSString stringWithFormat:@"(%@,%@)||%@ %@",currentLatitude,currentLongitude,placemark.country,myCity];



            if (self.myLabel.text.length > 0) {

                [self.locationManager stopUpdatingLocation];

            }

        }    

 

    }];


通过城市名定位经纬度:

CLGeocoder * geocoder = [[CLGeocoder alloc] init];
065  
066     [geocoder geocodeAddressString:_searchTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {
067          
068          
069         if(error)
070         {
071             NSLog(@"Geocoder error : %@",error);
072             return ;
073         }
074          
075         if([placemarks count] == 0)
076         {
077             NSLog(@"Could found the address.");
078             return ;
079         }
080          
081         CLPlacemark * placeMark = [placemarks objectAtIndex:0];
082          
083         _mapView.centerCoordinate = placeMark.location.coordinate;
084       
085         mAnnotationView * annotationView = [[mAnnotationView alloc] initWithCLLocationCoordinate2d:placeMark.location.coordinate title:placeMark.locality subTitle:[NSString stringWithFormat:@"%@-%@",placeMark.country,placeMark.administrativeArea]];
086       
087         [_mapView addAnnotation:annotationView];
088       
089         [_mapView selectAnnotation:annotationView animated:YES];
090       
091       
092         NSLog(@"country:%@,locality:%@,%@,%@",placeMark.country,placeMark.locality,placeMark.administrativeArea,placeMark.region);
093          
094         [indicatorView stopAnimating];
095       
096     }];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值