利用 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
分享到:
相关推荐
android通过GPS获取经纬度和具体城市.可以检测具体位置。
第一:当使用GPS定位时,最好不要使用getLastKnownLocation方法获得当前位置对象Location,因为该对 象可以在onLocationChanged的参数中由系统给予(根据文档,getLastKnownLocation有2方面功能:1. 获取当前地理...
完整的gps代码 以及几篇关于gps的资料 值得你拥有
android开发的一个GPS获取办法,有用的可以参照
通过手机自带的gps获取当前位置 并实现定位
这是我自己写的一个关于Gps使用的代码小例,主要是可以获取到全部的GPS信息,方便大家在这一方面的开发。
获得Android手机当前的GPS定位数据,保存到SQLite本地数据库中,个人的简单作品,不足之处请多执教
该系统通过接收GPS信号和INS信号,可以获取用户的位置信息和姿态信息,从而实现更准确的导航。 代码实现: 下面是一个简单的GPS/INS组合导航系统的代码示例,包括文档描述和代码实现。 文档描述: GPS/INS组合...
GPS定位的完整代码,可以显示你当前所在的经度、纬度、精确度、保证适用于任何人
GPS模块通信驱动代码,部分信息可扩展,时间信息提取完全。
获取Gps信息的程序源码.zip
gps码捕获,跟踪代码算法的仿真,这是基于gps的仿真推导
中国主要城市商圈 gps 相关信息,主要用于学习测试,请不要用于商业,谋利等,负责后果自负。
GPS导航系统源代码
用matlab编写的GPS信号生成代码例子
基于Android的GPS定位功能, 通过GPS定位获取地理位置坐标, 并且解析坐标信息向google请求坐标的详细位置(直接返回所在的位置信息,如城市名称等)
【Android】代码开启/关闭GPSDemo 相关文章:... 为了方便看效果,您可以在在onCreate中添加finish();方法,然后打开系统GPS设置页面再运行代码,这样就能看到GPS开关的变化了。
本程序实现了gps单点定位,代码简洁,功能强大,兼容数据能力强
嵌入式Linux GPS驱动代码,自动加速汽车速度获取嵌入式Linux GPS驱动代码欢迎大家下载使用
授时、测速、定位解算,关于GPS接收机的全部代码。