IOS开发GPS定位反查地名

阅读更多

最近开发一款ipad应用,需要根据所在位置不同优先显示不同地区的数据,像墨迹天气一样,网上例子不少,这里粘个代码备份一下:

#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:%@\n country:%@\n postalCode:%@\n ISOcountryCode:%@\n ocean:%@\n inlandWater:%@\n locality:%@\n subLocality:%@ \n administrativeArea:%@\n subAdministrativeArea:%@\n thoroughfare:%@\n subThoroughfare:%@\n",
              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);
 
}

 

用于显示当前设备所在地区的相关信息,也可以使用经纬度进行查询

使用CLLocationCoordinate2D coordinate;  

 coordinate.latitude =  42.611681;  

 coordinate.longitude = 144.161324;  

CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:coordinate.latitude longitude: coordinate.longitude];  

CLGeocoder *geocoder=[[CLGeocoder alloc] init];  

 [geocoder reverseGeocodeLocation:newLocation  

 completionHandler:^(NSArray *placemarks,             NSError *error)

代替

  CLGeocoder *geocoder=[[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:newLocation
                   completionHandler:^(NSArray *placemarks,
                                       NSError *error)
即可。

你可能感兴趣的:(IOS,GPS)