zoukankan      html  css  js  c++  java
  • iOS 根据经纬度反查 地名

    在iOS中 
    定位自己的当前位置,知道经纬度很简单,然后有些时候要知道地名,apple 也有了现成的api直接调用就可以(以下方法是iOS5.0以上的,现在基本都忽略了 iOS5.0以下的设备)

    #pragma mark -
    #pragma mark CLLocationManagerDelegate
     
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
     
      
       CLGeocoder *geocoder=[[CLGeocoder alloc] init];
        [geocoder reverseGeocodeLocation:newLocation
                       completionHandler:^(NSArray *placemarks,
                                           NSError *error)
        {
            CLPlacemark *placemark=[placemarks objectAtIndex:0];
            NSLog(@"name:%@
     country:%@
     postalCode:%@
     ISOcountryCode:%@
     ocean:%@
     inlandWater:%@
     locality:%@
     subLocality:%@ 
     administrativeArea:%@
     subAdministrativeArea:%@
     thoroughfare:%@
     subThoroughfare:%@
    ",
                  placemark.name,
                  placemark.country,
                   placemark.postalCode,
                   placemark.ISOcountryCode,
                   placemark.ocean,
                   placemark.inlandWater,
                   placemark.administrativeArea,
                   placemark.subAdministrativeArea,
                  placemark.locality,
                   placemark.subLocality,
                   placemark.thoroughfare,
                   placemark.subThoroughfare);
         }];
    	if (wasFound) return;
        wasFound = YES;
        
    	CLLocationCoordinate2D loc = [newLocation coordinate];
    	
    	strLatitude = [NSString stringWithFormat: @"%f", loc.latitude];
    	strLongitude = [NSString stringWithFormat: @"%f", loc.longitude];
    	
    	NSLog(@"strLatitude==%@,strLongitude==%@",strLatitude,strLongitude);
    	 
    }
    

     log 是:

     name:市民广场
     country:中国
     postalCode:(null)
     ISOcountryCode:CN
     ocean:(null)
     inlandWater:(null)
     locality:广东省
     subLocality:(null) 
     administrativeArea:深圳市
     subAdministrativeArea:福田区
     thoroughfare:市民广场
     subThoroughfare:(null)

    第二种方法就是 我们已经知道了一个经纬度 然后反查地名:

    自己写个方法:

    -(void)change{
        //22.540681,=114.061324
        CLLocationCoordinate2D coordinate;
        coordinate.latitude =  22.540681;
        coordinate.longitude = 114.061324;
        CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:coordinate.latitude longitude: coordinate.longitude];
        CLGeocoder *geocoder=[[CLGeocoder alloc] init];
        [geocoder reverseGeocodeLocation:newLocation
                       completionHandler:^(NSArray *placemarks,
                                           NSError *error)
         {
             CLPlacemark *placemark=[placemarks objectAtIndex:0];
             NSLog(@"我我的:%@
     country:%@
     postalCode:%@
     ISOcountryCode:%@
     ocean:%@
     inlandWater:%@
     locality:%@
     subLocality:%@ 
     administrativeArea:%@
     subAdministrativeArea:%@
     thoroughfare:%@
     subThoroughfare:%@
    ",
                   placemark.name,
                   placemark.country,
                   placemark.postalCode,
                   placemark.ISOcountryCode,
                   placemark.ocean,
                   placemark.inlandWater,
                   placemark.administrativeArea,
                   placemark.subAdministrativeArea,
                   placemark.locality,
                   placemark.subLocality,
                   placemark.thoroughfare,
                   placemark.subThoroughfare);
         }];
    
    }

    nslog 的结果和上面的一样。

  • 相关阅读:
    CompoundButton.OnCheckedChangeListener与RadioGroup.OnCheckedChangeListener冲突
    C# String.Format格式化json字符串中包含"{" "}"报错问题
    在IHttpHandler中获取session
    你真的会玩SQL吗?删除重复数据且只保留一条
    activity结束之后刷新之前的activity的内容
    jQuery打造智能提示插件二(可编辑下拉框)
    byte数组转float 以及byte转其他类型时为什么要&0xff
    为什么byte的取值范围是-128到127
    MySQL修改表、字段、库的字符集及字符集说明
    MySQL分布式jdbc连接
  • 原文地址:https://www.cnblogs.com/zhaoguowen/p/4186295.html
Copyright © 2011-2022 走看看