900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > iOS开发:通过经纬度获得城市 省份等信息

iOS开发:通过经纬度获得城市 省份等信息

时间:2018-12-09 08:47:17

相关推荐

iOS开发:通过经纬度获得城市 省份等信息

iOS系统自带定位,用CLLocationManager就可以轻松的实现定位的操作,获得的是一组经纬度,当然,也可以根据给出的经纬度获取相应的省份、城市、街道等信息,下面就看一个根据经纬度获得城市的demo:

因为获取经纬度需要CLLocationManager类,而这个类包含在CoreLocation框架中,获取城市信息需要mapKit框架,所以需要首先在工程中导入这两个框架:

导入框架的步骤:选择1.target——2.Build Phases——3.Link Binary With Libraries ——4.点击“+”号:如图所示步骤:

点击加号之后在搜索框里输入相应的框架,即可搜索到,如图所示:

下面就该写代码了,首先在视图控制器中导入:

#import<CoreLocation/CoreLocation.h>#import<MapKit/MapKit.h>

两个头文件,然后.m中的具体代码如下:

#import"ANNViewController.h"@interfaceANNViewController()@property(strong,nonatomic)IBOutletUILabel*longitude;@property(strong,nonatomic)IBOutletUILabel*latitude;@property(strong,nonatomic)IBOutletUILabel*location;@property(strong,nonatomic)CLLocationManager*locationManager;@end@implementationANNViewController-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.self.view.backgroundColor=[UIColorwhiteColor];//创建CLLocationManager对象self.locationManager=[[CLLocationManageralloc]init];//设置代理为自己self.locationManager.delegate=self;}-(IBAction)locationButton:(UIButton*)sender{[self.locationManagerstartUpdatingLocation];}-(void)locationManager:(CLLocationManager*)managerdidUpdateToLocation:(CLLocation*)newLocationfromLocation:(CLLocation*)oldLocation{//将经度显示到label上self.longitude.text=[NSStringstringWithFormat:@"%lf",newLocation.coordinate.longitude];//将纬度现实到label上self.latitude.text=[NSStringstringWithFormat:@"%lf",newLocation.coordinate.latitude];//获取当前所在的城市名CLGeocoder*geocoder=[[CLGeocoderalloc]init];//根据经纬度反向地理编译出地址信息[geocoderreverseGeocodeLocation:newLocationcompletionHandler:^(NSArray*array,NSError*error){if(array.count>0){CLPlacemark*placemark=[arrayobjectAtIndex:0];//将获得的所有信息显示到label上self.location.text=placemark.name;//获取城市NSString*city=placemark.locality;if(!city){//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)city=placemark.administrativeArea;}NSLog(@"city=%@",city);}elseif(error==nil&&[arraycount]==0){NSLog(@"Noresultswerereturned.");}elseif(error!=nil){NSLog(@"Anerroroccurred=%@",error);}}];//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新[managerstopUpdatingLocation];}

主要就是直辖市的城市获得需要拐个弯,iOS7添加了一个新的方法,代替了上面这个方法:

-(void)locationManager:(CLLocationManager*)managerdidUpdateLocations:(NSArray*)locations{NSLog(@"longitude=%f",((CLLocation*)[locationslastObject]).coordinate.longitude);NSLog(@"latitude=%f",((CLLocation*)[locationslastObject]).coordinate.latitude);[managerstopUpdatingLocation];}

后面的处理和上面的方法一样,大家可以看一下。

另外还有一些CLGeocoder的属性如下:

@property(nonatomic,readonly)NSDictionary*addressDictionary;//addressdictionaryproperties@property(nonatomic,readonly)NSString*name;//eg.AppleInc.@property(nonatomic,readonly)NSString*thoroughfare;//streetaddress,eg.1InfiniteLoop@property(nonatomic,readonly)NSString*subThoroughfare;//eg.1@property(nonatomic,readonly)NSString*locality;//city,eg.Cupertino@property(nonatomic,readonly)NSString*subLocality;//neighborhood,commonname,eg.MissionDistrict@property(nonatomic,readonly)NSString*administrativeArea;//state,eg.CA@property(nonatomic,readonly)NSString*subAdministrativeArea;//county,eg.SantaClara@property(nonatomic,readonly)NSString*postalCode;//zipcode,eg.95014@property(nonatomic,readonly)NSString*ISOcountryCode;//eg.US@property(nonatomic,readonly)NSString*country;//eg.UnitedStates@property(nonatomic,readonly)NSString*inlandWater;//eg.LakeTahoe@property(nonatomic,readonly)NSString*ocean;//eg.PacificOcean@property(nonatomic,readonly)NSArray*areasOfInterest;//eg.GoldenGatePark

完整的工程如下:

附件下载地址:/winann/TestLocation.git

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。