利用 iPhone、iPad 自带的 GPS 信息可以获得当前位置坐标,但直接高速用户坐标是不行的。CocoaChina 会员"laigb"分享了通过 GPS 信息获取当前所在城市名的代码,希望对苹果开发者有所帮助。
#import "testLocationVIEwController.h"
@implementation testLocationVIEwController
@synthesize locationmanger = _locationmanger;
- (voID)dealloc
{
[super dealloc];
[_locationmanger release];
}
- (voID)dIDReceiveMemoryWarning
{
// Releases the vIEw if it doesn't have a supervIEw.
[super dIDReceiveMemoryWarning];
// Release any cached data,images,etc that aren't in use.
}
#pragma mark - VIEw lifecycle
- (voID)vIEwDIDLoad
{
[super vIEwDIDLoad];
cityLabel = [[[UILabel alloc] initWithFrame: CGRectMake(20,30,100,100)] autorelease];
[self.vIEw addSubvIEw: cityLabel];
/*
self.locationmanger = [[CLLocationManager alloc] init];
_locationmanger.delegate = self;
_locationmanger.desiredAccuracy = kCLLocationAccuracyBest;
[_locationmanger startUpdatingLocation];
*/
_locationmanger = [[CLLocationManager alloc] init];
_locationmanger.delegate = self;
_locationmanger.distanceFilter = 1;
_locationmanger.desiredAccuracy = kCLLocationAccuracyBest;
[_locationmanger startUpdatingLocation];
}
- (voID)vIEwDIDUnload
{
[super vIEwDIDUnload];
// Release any retained subvIEws of the main vIEw.
// e.g. self.myOutlet = nil;
}
- (BOol)shouldautorotatetoInterfaceOrIEntation:(UIInterfaceOrIEntation)interfaceOrIEntation
{
// Return YES for supported orIEntations
return YES;
}
- (voID)locationManager:(CLLocationManager *)manager
dIDFailWithError:(NSError *)error
{
}
- (voID)locationManager:(CLLocationManager *)manager dIDUpdatetoLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationdistance l = newLocation.coordinate.latitude;//得到经度
CLLocationdistance v = newLocation.coordinate.longitude;//得到纬度
NSLog(@"%f %f",l,v);
CLLocation *new = [[CLLocation alloc] initWithLatitude: 11.0 longitude: 12.0];
CLLocationdistance u = [newLocation distanceFromLocation: new];
NSLog(@"%f",u);
[self startedReverseGeoderWithLatitude: l longitude: v];
}
- (voID)startedReverseGeoderWithLatitude:(double)latitude longitude:(double)longitude{
CLLocationCoordinate2D coordinate2D;
coordinate2D.longitude = longitude;
coordinate2D.latitude = latitude;
MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate2D];
geoCoder.delegate = self;
[geoCoder start];
}
#pragma mark -
- (voID)reverseGeocoder:(MKReverseGeocoder *)geocoder dIDFindplacemark:(MKPlacemark *)placemark
{
Nsstring *subthroung=placemark.subThoroughfare;
Nsstring *local=placemark.locality;
NSLog(@"城市名:%@-%@-%@",placemark.locality,local,subthroung);
if (local) {
[cityLabel setText:local];
}
}
- (voID)reverseGeocoder:(MKReverseGeocoder *)geocoder dIDFailWithError:(NSError *)error
{
}
@end
总结以上是内存溢出为你收集整理的通过 GPS 信息获取当前所在城市名的代码全部内容,希望文章能够帮你解决通过 GPS 信息获取当前所在城市名的代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)