下面介绍的时地图定位和位置反编码
//
// AppDelegate.h
// LocationDemo
//
#import
#import
#import
&#64;interface AppDelegate : UIResponder<UIApplicationDelegate,CLLocationManagerDelegate,MKReverseGeocoderDelegate>
&#64;property (strong, nonatomic) UIWindow *window;
&#64;end
//
// AppDelegate.m
// LocationDemo
//
#import "AppDelegate.h"
&#64;implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window &#61; [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor &#61; [UIColor whiteColor];
[self.window makeKeyAndVisible];
CLLocationManager *locationManager &#61; [[CLLocationManager alloc] init];
//设置过滤信息
// [locationManager setDistanceFilter:<#(CLLocationDistance)#>]
//设置定位的精度
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
locationManager.delegate &#61; self;
//开始实时定位
[locationManager startUpdatingLocation];
return YES;
}
//实时定位调用的方法, 6.0过期的方法
#pragma mark - CLLocationManager delegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D coordinate &#61; newLocation.coordinate;
NSLog(&#64;"经度&#xff1a;%f,纬度&#xff1a;%f",coordinate.longitude,coordinate.latitude);
//停止实时定位
[manager stopUpdatingLocation];
//计算两个位置的距离
// float distance &#61; [newLocation distanceFromLocation:oldLocation];
// NSLog(&#64;"%f",distance);
//------------------位置反编码---5.0之后使用-----------------
CLGeocoder *geocoder &#61; [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error){
for (CLPlacemark *place in placemarks) {
NSLog(&#64;"name,%&#64;",place.name); // 位置名
NSLog(&#64;"thoroughfare,%&#64;",place.thoroughfare); // 街道
NSLog(&#64;"subThoroughfare,%&#64;",place.subThoroughfare); // 子街道
NSLog(&#64;"locality,%&#64;",place.locality); // 市
NSLog(&#64;"subLocality,%&#64;",place.subLocality); // 区
NSLog(&#64;"country,%&#64;",place.country); // 国家
}
}];
//----------------------位置反编码--5.0之前的使用-----------------
MKReverseGeocoder *mkgeocoder &#61; [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
mkgeocoder.delegate &#61; self;
[mkgeocoder start];
}
//6.0之后新增的位置调用方法
//- (void)locationManager:(CLLocationManager *)manager
// didUpdateLocations:(NSArray *)locations {
// for (CLLocation *location in locations) {
// NSLog(&#64;"%&#64;",location);
// }
//
// //停止实时定位
// [manager stopUpdatingLocation];
//
//}
#pragma mark - MKReverseGeocoder delegate
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder
didFindPlacemark:(MKPlacemark *)place {
NSLog(&#64;"---name,%&#64;",place.name); // 位置名
NSLog(&#64;"---thoroughfare,%&#64;",place.thoroughfare); // 街道
NSLog(&#64;"---subThoroughfare,%&#64;",place.subThoroughfare); // 子街道
NSLog(&#64;"---locality,%&#64;",place.locality); // 市
NSLog(&#64;"---subLocality,%&#64;",place.subLocality); // 区
NSLog(&#64;"---country,%&#64;",place.country); // 国家
}
&#64;end
原文地址&#xff1a;http://blog.sina.com.cn/s/blog_aeb8e4450101avl5.html