iOS将经纬度解析为地址

本文转自:http://www.cnblogs.com/humutian/archive/2012/08/07/chapter11.html


关于定位我也是通过学习ios5 pragram cookbook中的第六章学习才得以解决但是在第六章中使用的地理位置反编码技术只能在ios5系统中才能实现在低版本的ios是不能兼容的所以在里我使用了两种方法来实现定位。

 其定位有3种方式:

1,GPS,最精确的定位方式

2,蜂窝基站三角定位,这种定位在信号基站比较秘籍的城市比较准确。

3,Wifi,这种方式貌似是通过网络运营商的数据库得到的数据,在3种定位种最不精确

 

首先你要在你的Xcode中添加两个连接库,MapKit和CoreLocation,如图

core location提供了定位功能,能定位装置的当前坐标,同时能得到装置移动信息,最重要的类是CLLocationManager,定位管理

 首相我们先定位,在手机上得到手机在地球上所处的经度和维度通过经度下面以一段是定位的启动程序

复制代码
 if([CLLocationManager locationServicesEnabled])
    {
        self.myLocationManager = [[CLLocationManager alloc]init];
        self.myLocationManager.delegate = self;
        self.myLocationManager.purpose = @"To provide functionality based on user's current location.";
//        NSLog(@"PURPOSE = %@",self.myLocationManager.purpose);
         //选择定位的方式为最优的状态,他又四种方式在文档中能查到
        self.myLocationManager.desiredAccuracy=kCLLocationAccuracyBest;
         //发生事件的最小距离间隔
        self.myLocationManager.distanceFilter = 1000.0f;
        [self.myLocationManager startUpdatingLocation];
               
    }
复制代码

在iOS4中的解决方案

很多时候我们需要把它反向编码成普通人能读懂的地理位置描述如:X国XX市XXX区XXX街道XX号,这就需要用到MapKit中的一个地理位置反向编码工具:MKReverseGeocoder,首相我们要利用CLLocationManagerDelegate,因为将坐标信息发到服务器再反回来需要一定的时间,所以为了防止阻塞,发出信息后并不知到什么时候会返回信息,信息返回时会通知委托方法。这里实现这个类主要时为了实现2个方法如下

复制代码
-(void)locationManager:(CLLocationManager *)manager
      didFailWithError:(NSError *)error
{
   
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder
      didFailWithError:(NSError *)error
{
    NSLog(@"reverse geocoder error: %@", [error description]);
}
复制代码

 

didFailWithError这个方法是来处理返回错误信息的,didFindPlacemark则是地理信息返回了,地理信息包含在placemark里面,此对象中包含国家,城市,区块,街道等成员变量。

 

然后初始化一个MKReverseGeocoder(地理反向编码)

MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc]initWithCoordinate:self.myLocation.coordinate];
    geocoder.delegate = self;
     //启动gecoder
    [geocoder start];

 

而在ios5中的解决方案

在IOS5中不用MKReverseGeocoder反向编码,而是使用CLLocationManagerDelegate直接根据经度和维度反向编码生成地理位置,代码在下面的注释部分。

需要注意的是[self.myLocationManager startUpdatingLocation];这里的更新可能会在的你终端中输出好几次,所以在得到地址之后应该及时[self.myLocationManager stopUpdatingLocation];

复制代码
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize myLocationManager;
@synthesize myGecoder;
@synthesize myLocation;

#pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    static int i = 0;
    i++;
    NSLog(@"I = %d", i);
    NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
    NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
    self.myLocation = newLocation;
//    self.myGecoder = [[CLGeocoder alloc] init];
//    [self.myGecoder reverseGeocodeLocation:self.myLocation completionHandler:^(NSArray *placemarks, NSError *error)
//     {
//         if(error == nil && [placemarks count]>0)
//         {
//             CLPlacemark *placemark = [placemarks objectAtIndex:0];
//            
//             NSLog(@"Country = %@", placemark.country);
//             NSLog(@"Postal Code = %@", placemark.postalCode);
//             NSLog(@"Locality = %@", placemark.locality);
//             NSLog(@"address = %@",placemark.name);
//             NSLog(@"administrativeArea = %@",placemark.administrativeArea);
//             NSLog(@"subAdministrativeArea = %@",placemark.subAdministrativeArea);
//             NSLog(@"locality = %@", placemark.locality);
//             NSLog(@"thoroughfare = %@", placemark.thoroughfare);
//         }
//         else if(error==nil && [placemarks count]==0){
//             NSLog(@"No results were returned.");           
//         }
//         else if(error != nil) {
//             NSLog(@"An error occurred = %@", error);
//         }
//     }];
//   [self.myLocationManager stopUpdatingLocation];
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc]initWithCoordinate:self.myLocation.coordinate];
    geocoder.delegate = self;
    [geocoder start];
}

-(void)locationManager:(CLLocationManager *)manager
      didFailWithError:(NSError *)error
{
   
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder
      didFailWithError:(NSError *)error
{
    NSLog(@"reverse geocoder error: %@", [error description]);
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder
      didFindPlacemark:(MKPlacemark *)placemark
{
    NSLog(@"Country = %@", placemark.country);
    NSLog(@"Postal Code = %@", placemark.postalCode);
    NSLog(@"Locality = %@", placemark.locality);
    NSLog(@"address = %@",placemark.name);
    NSLog(@"administrativeArea = %@",placemark.administrativeArea);
    NSLog(@"subAdministrativeArea = %@",placemark.subAdministrativeArea);
    NSLog(@"locality = %@", placemark.locality);
    NSLog(@"thoroughfare = %@", placemark.thoroughfare); 
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
   
    if([CLLocationManager locationServicesEnabled])
    {
        self.myLocationManager = [[CLLocationManager alloc]init];
        self.myLocationManager.delegate = self;
        self.myLocationManager.purpose = @"To provide functionality based on user's current location.";
//        NSLog(@"PURPOSE = %@",self.myLocationManager.purpose);
        self.myLocationManager.desiredAccuracy=kCLLocationAccuracyBest;
        self.myLocationManager.distanceFilter = 1000.0f;
        [self.myLocationManager startUpdatingLocation];
               
    }
    else {
        NSLog(@"Location services are not enabled");
    }
  
}

- (void)viewDidUnload
{
    [super viewDidUnload];
   
    self.myLocationManager = nil;
    self.myGecoder = nil;
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void)dealloc
{
    [self.myLocation release];
    [self.myLocationManager release];
    [self.myGecoder release];
    [super dealloc];
}

@end
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值