在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:%@\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); }
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(@"我我的:%@\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); }]; }nslog 的结果和上面的一样。