iOS8 根據經緯度顯示地名 – iPhone手機開發技術文章 iPhone軟體開發教學課程

我們很多時候做項目的時候都選用通過手機獲取定位,然後傳數據給後臺,這是iOS8根據經緯度顯示地名的demo

//1.導入框架

//

// NearByViewController.m

// nearDemo

//

// Created by apple on 14/12/5.

// Copyright (c) 2014年 youdianshang. All rights reserved.

//

#import

#import

@interface
NearByViewController ()

{

NSUserDefaults *defaluts;

NSString * jingduStr;

NSString *weiduStr;

NSString *LocationAddress;

}

@property(nonatomic,retain)CLLocationManager *locationManager;

@property(strong,nonatomic)UILabel *positionLabel;

/*

經緯度相關設置

*/

-(void)getLocationAll

{

self.locationManager = [[CLLocationManager
alloc]init];

_locationManager.delegate =
self;

_locationManager.desiredAccuracy =
kCLLocationAccuracyBest;

_locationManager.distanceFilter =
10;

[_locationManager
requestAlwaysAuthorization];//添加這句

[_locationManager
startUpdatingLocation];

}

/*

//通過經緯度得到當前地理位置

-(void)change{

defaluts=[NSUserDefaults
standardUserDefaults];

CLLocationCoordinate2D coordinate;

coordinate.latitude = [[NSString
stringWithFormat:@”%@”,jingduStr]
floatValue];

coordinate.longitude = [[NSString
stringWithFormat:@”%@”,weiduStr]
floatValue];

CLLocation *newLocation=[[CLLocation
alloc]initWithLatitude:coordinate.latitude
longitude: coordinate.longitude];

CLGeocoder *geocoder=[[CLGeocoder
alloc] init];

[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray* placemarks,
NSError* error)

{

MKPlacemark*placemark = [placemarks
objectAtIndex:0];

LocationAddress = [NSString
stringWithFormat:@”%@%@%@%@”, placemark.locality, placemark.subLocality,placemark.thoroughfare,
placemark.subThoroughfare];

[defaluts
setObject:LocationAddress
forKey:@”address”];

NSLog(@”終極目標:1:%@2:%@3:%@4:%@”, placemark.locality,
placemark.subLocality,placemark.thoroughfare,placemark.subThoroughfare);

}];

#pragma mark–實現CLLocationManagerDelegate的代理方法,獲取地理位置(鑌)===========================================================================

//(1)獲取到位置數據,返回的是一個CLLocation的數組,一般使用其中的一個

– (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

CLLocation *currLocation = [locations
lastObject];

NSLog(@”經度=%f
緯度=%f
高度=%f”, currLocation.coordinate.latitude, currLocation.coordinate.longitude,
currLocation.altitude);

jingduStr =[NSString
stringWithFormat:@”%f”, currLocation.coordinate.latitude];

weiduStr =[NSString
stringWithFormat:@”%f”, currLocation.coordinate.longitude];

NSLog(@”jijijijijij:%@”,jingduStr);

[self change];

}

//(2)獲取用戶位置數據失敗的回調方法,在此通知用戶

– (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

if ([error code] ==
kCLErrorDenied)

{

//訪問被拒絕

}

if ([error
code] == kCLErrorLocationUnknown) {

//無法獲取位置信息

}

}

發佈留言