热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iPhone开发之地图定位(CoreLocation和Mapkit框架)简易编程

下面介绍的时地图定位和位置反编码 // //  AppDelegate.h //  LocationDemo // #import #import #import @interfac

下面介绍的时地图定位和位置反编码

//

//  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


推荐阅读
author-avatar
董可芳妍_731
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有