iOS上地图通过地理位置经纬度找出城市等信息

iOS上地图通过地理位置经纬度找出城市等信息

 

通过经纬度获得城市等信息需要添加两个协议:

#import <UIKit/UIKit.h>

#import “MapKit/MapKit.h”

@interface GPSViewController : UIViewController<CLLocationManagerDelegate,MKReverseGeocoderDelegate>

{

CLLocationManager *locationManager;

CLLocationCoordinate2D mylocation;//存储定位得到的精度和纬度

CLGeocoder *Geocoder;//CLGeocoder

}

@end

其中MKReverseGeocoderDelegate是获得城市信息的关键,接下来是使用方法,在随便一个位置我加了个UIButton,当点击时会触发下面的事件:

PS.修改,5.0以后MKReverseGeocoderDelegate协议不可用了,下面是获取城市信息的新方法,还是用CLGeocoder,注意的是参数是CLLocation

 

 

 

下面是老方法,也可以用:

-(IBAction)activeGPS:(id)sender{

locationManager = [[CLLocationManager alloc] init];//创建位置管理器

locationManager.delegate = self; //设置代理

locationManager.desiredAccuracy = kCLLocationAccuracyBest; //指定需要的精度级别

locationManager.distanceFilter = kCLDistanceFilterNone; //设置距离筛选器 ,动1000米才有反应

[locationManager startUpdatingLocation]; //启动位置管理器

CLLocationDegrees latitude = locationManager.location.coordinate.latitude; //float也行,获得当前位置的纬度.location属性获

CLLocationDegrees longitude = locationManager.location.coordinate.longitude;

NSLog(@”latitude%f”,latitude);

NSLog(@”longitude%f”,longitude);

mylocation.latitude=latitude;

mylocation.longitude=longitude;

[self showWithlocation:mylocation];

}

这个方法的目的就是定位并获得经纬度赋值给了mylocation,并调用  [self showWithlocation:mylocation]方法,获得信息,下面是showWithlocation方法,参数就是传进去的my location:

- (void)showWithlocation:(CLLocationCoordinate2D)location {

Geocoder=[[CLGeocoder alloc]init];

CLGeocodeCompletionHandler handler = ^(NSArray *place, NSError *error) {

for (CLPlacemark *placemark in place) {

NSString *cityStr=[placemark.addressDictionary objectForKey:@"City"];

NSLog(@”city %@”,cityStr);

NSString *Street=[placemark.addressDictionary objectForKey:@"Street"];

NSLog(@”Street %@”,Street);

NSString *State=[placemark.addressDictionary objectForKey:@"State"];

NSLog(@”State %@”,State);

NSString *ZIP=[placemark.addressDictionary objectForKey:@"ZIP"];

NSLog(@”ZIP %@”,ZIP);

NSString *Country=[placemark.addressDictionary objectForKey:@"Country"];

NSLog(@”Country %@”,Country);

NSString *CountryCode=[placemark.addressDictionary objectForKey:@"CountryCode"];

NSLog(@”CountryCode %@”,CountryCode);

break;

}

};

CLLocation *loc = [[CLLocation alloc] initWithLatitude:location.latitude longitude:location.longitude];

[Geocoder reverseGeocodeLocation:loc completionHandler:handler];

}

 应该可以看到各种信息了。
补充一下,其实placemark.addreddDictionary打印出来的内容是这样的:

2012-06-19 10:03:35.375 JinjiliApp[4403:707] dictionary {

    City = “U5317U4eacU5e02″;

    Country = “U4e2dU56fd”;

    CountryCode = CN;

    FormattedAddressLines =     (

        “U4e2dU56fdU5317U4eacU5e02U671dU9633U533aU5609U94edU6850U57ceDU5357U533a(U897fU4e00U53f7U95e8)U5609U94edU6850U57ceDU5357U533a(U897fU4e00U53f7U95e8)”

    );

    Name = “U5609U94edU6850U57ceDU5357U533a(U897fU4e00U53f7U95e8)”;

    SubLocality = “U671dU9633U533a”;

    Thoroughfare = “U5609U94edU6850U57ceDU5357U533a(U897fU4e00U53f7U95e8)”;

}

这里想提取FormattedAddressLines和Name什么的都是空,查了下,原来FormattedAddressLines是个数组=.=

2012-06-19 10:11:50.162 JinjiliApp[4418:707] FormattedAddressLines 中国北京市朝阳区嘉铭桐城D南区(西一号351227250)嘉铭桐城D南区(西一号门)

如果不通过addressDictionary,placemark也有其他属性,取具体地址可以直接调用thoroughfare

MKPlacemark:

@property (nonatomic, readonly) NSDictionary *addressDictionary; //地址字典

@property (nonatomic, readonly) NSString *thoroughfare; //街道名——“科韻路”

@property (nonatomic, readonly) NSString *subThoroughfare; // 門牌號——“18號”

@property (nonatomic, readonly) NSString *locality; //城市——“廣州市”

@property (nonatomic, readonly) NSString *subLocality; //區縣——“天河區”

@property (nonatomic, readonly) NSString *administrativeArea; //身份——“廣東省”

@property (nonatomic, readonly) NSString *subAdministrativeArea; //沒獲取到,不知道是什麼東東

@property (nonatomic, readonly) NSString *postalCode; //郵政編碼——不知道是什麼原因,這裡沒獲取到

@property (nonatomic, readonly) NSString *country; //國家——“中國”

@property (nonatomic, readonly) NSString *countryCode; //國家代碼——“CN”


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值