iphone中GPS精确定位及反向获取地址信息
来源:互联网 发布:vb 串口扫描枪 编辑:程序博客网 时间:2024/05/03 06:05
刚开始习惯性用CoreLocation.framework框架中的CLLocationManagerDelegate,获取当前的经纬度,再用经纬度反向解析出当前位置。代码如下:
.h文件
#import<UIKit/UIKit.h>
#import<CoreLocation/CoreLocation.h>
#import<MapKit/MapKit.h>
@interface FirstViewController :UIViewController <CLLocationManagerDelegate,MKMapViewDelegate>{
CLLocationManager *lm;
}
@property (retain,nonatomic) IBOutletUILabel *locationLabel;
@property (retain,nonatomic) IBOutletMKMapView *map;
.m文件
#import"FirstViewController.h"
@implementation FirstViewController;
@synthesize locationLabel;
@synthesize map;
- (void)viewDidLoad
{
[superviewDidLoad];
lm = [[CLLocationManageralloc]init];
lm.delegate =self;
lm.desiredAccuracy =kCLLocationAccuracyBest;//设置最高精度
lm.distanceFilter =kCLDistanceFilterNone;
[lmstartUpdatingHeading];//方向
[lmstartUpdatingLocation];//位置
map.delegate =self;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"定位出错");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
// //解析并获取当前坐标对应得地址信息
if ([[[UIDevicecurrentDevice] systemVersion]floatValue] >= 5.0) {
// NSLog(@"iphone :%f",[[[UIDevice currentDevice] systemVersion] floatValue]);
CLGeocoder *clGeoCoder = [[CLGeocoderalloc] init];
CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
{
for (CLPlacemark * placeMarkin placemarks){
self.locationLabel.text = placeMark.name;
}
};
[clGeoCoder reverseGeocodeLocation:newLocationcompletionHandler:handle];
[clGeoCoder release];
[manager stopUpdatingLocation];
}else {
//[self startedReverseGeoderWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
NSLog(@"iphone :%f",[[[UIDevicecurrentDevice] systemVersion]floatValue]);
}
}
(void)viewDidLoad
{
[superviewDidLoad];
[mapsetHidden:YES];
map.showsUserLocation =YES;
map.delegate = self;
}
因为要调用MKMapViewDelegate委托里的方法,需实例化一个mapview,如果在实际项目过程中不想显示地图,可以设置位Hidden。
将map.showsUserLocation设置为YES,才能开启定位获取到用户当前地理位置的信息。获取后再设置为NO即可停止定位。
注意下面这个方法是MKMapViewDelegate委托里的方法,而不是CLLocationManagerDelegate委托里的方法。反向解析的代码基本一致。
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
//CLLocationCoordinate2D coords = userLocation.location.coordinate;
CLLocation * newLocation = userLocation.location;
// NSLog(@"2::::::%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
// //解析并获取当前坐标对应得地址信息
if ([[[UIDevicecurrentDevice] systemVersion]floatValue] >= 5.0) {
CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
{
for (CLPlacemark * placeMark in placemarks)
{
self.locationLabel.text = placeMark.name;
map.showsUserLocation = NO;
}
};
[clGeoCoderreverseGeocodeLocation:newLocation completionHandler:handle];
[clGeoCoderrelease];
}
}
- iphone中GPS精确定位及反向获取地址信息
- iphone中GPS精确定位及反向获取地址信息
- iphone获取GPS的数据信息并定位
- Android地图获取详细街道地址信息 精确定位
- 使用GPS获取定位信息
- Unity3d,获取GPS定位信息
- Android中使用GPS和NetWork获取定位信息
- Android中使用GPS和NetWork获取定位信息
- android 获取GPS定位(原文地址)
- 使用j2me获取gps定位信息
- android网络-GPS获取定位信息
- Android 获取GPS和网络定位信息
- 利用手机gps获取经纬信息定位
- GPS实时定位、获取基站信息
- 安信可A7模块GPS-编程获取GPS定位信息
- Andreoid实现GPS/AGPS定位及获取附近多个基站信息
- Android获取位置信息 - GPS定位 Network定位
- android中获取位置(二)--利用反向地址编码获取具体位置信息
- 操作系统_再识(Linux设备模型之input子系统详解)
- 在Tomcat上使用日志分析工具Awstats
- 常用电平标准(TTL、CMOS、LVTTL、LVCMOS、ECL、PECL、LVPECL、RS232)
- JAVA AES加密 对应的 C# 方法
- 中国各省市编号(具体到二级市、区、县)
- iphone中GPS精确定位及反向获取地址信息
- 2013/05/29面试
- highcharts图表 line_basic封装
- 20年嵌入式领域开发经验精华
- 程序员40岁之后怎么办
- LSI SAS 1068E Raid CentOS 5.5 安装实例浪潮NF5220系列
- Shell 编程基础
- 操作系统_再识(Linux设备文件简介)
- apktool + smali + dex2jar + jdgui实现apk反编译