`
ydbc
  • 浏览: 711147 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

iphone获取 用户坐标详细信息 MKPlacemark userLocation MKMapView

 
阅读更多


- (IBAction)reverseGeocodeCurrentLocation

{

self.reverseGeocoder =

[[[MKReverseGeocoderalloc]initWithCoordinate:mapView.userLocation.location.coordinate]autorelease];

reverseGeocoder.delegate =self;

[reverseGeocoder start];

}


- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error

{

NSString *errorMessage = [error localizedDescription];

UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"Cannot obtain address."

message:errorMessage

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles:nil];

[alertView show];

[alertView release];

}


- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark

{

NSLog(@"reverse finished...");
NSLog(@"country:%@",placemark.country);
NSLog(@"countryCode:%@",placemark.countryCode);
NSLog(@"locality:%@",placemark.locality);
NSLog(@"subLocality:%@",placemark.subLocality);
NSLog(@"postalCode:%@",placemark.postalCode);
NSLog(@"subThoroughfare:%@",placemark.subThoroughfare);
NSLog(@"thoroughfare:%@",placemark.thoroughfare);
NSLog(@"administrativeArea:%@",placemark.administrativeArea);
self.locationInfo.locationName = [NSString stringWithFormat:@"%@%@,%@%@,%@",
placemark.country,
placemark.administrativeArea,
placemark.locality,
placemark.subLocality,
placemark.thoroughfare];
NSLog(@"administrativeArea:%@",self.locationInfo.locationName);
//[self.target performSelector:self.callBack withObject:@"0"];
geocoder = nil;

}


- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

// we have received our current location, so enable the "Get Current Address" button

[getAddressButton setEnabled:YES];

}



下面是转载的一些常用信息


iOS地图位置开发

iPhone SDK提供了三个类来管理位置信息:CLLocation CLLocationManager 和 CLLHeading(不常用)。除了使用GPS来获取当前的位置信息外,iPhone也可以基于WiFi基站和无线发射塔来获得位置信息。GPS的精度最高,可以精确到米级别,但是也最耗电。


------------CLLocation
CLLocation类代表一个位置信息,其中还包括了方向和速度。比如我在长安街188号以5公里/小时的速度往西走。CLLocation具有下面的属性和方法:
@property CLLocationCoordinate2D coordinate; //以经度和纬度表示的位置信息
@property CLLocationDistance altitude; //海拔
@property CLLocationAccuracy horizontalAccuracy; //水平精度(如:精确到米)
@property CLLocationAccuracy verticalAccuracy; //垂直精度
@property CLLocationDirection course; //方向
@property CLLocationSpeed speed; //速度
-(NSDate *)timeStamp;
//两个位置之间的距离
-(CLLocationDistance)distanceFromLocation:(CLLocation *)location;


-------------CLLocationManager
CLLocationManager类管理和提供位置服务。它的属性和方法有:
@property CLLocation *location; //位置
@property id<CLLocationManagerDelegate> delegate;
@property CLLocationDistance distanceFilter; //距离过滤,比如:500以内
@property CLlocationAccuracy verticalAccuracy; //垂直精度
-(void) startUpdatingLocation; //开始更新位置(比如:你在往某个地方走)
-(void)stopUpdatingLocation; //停止更新位置
-(void)startUpdatingHeading; //开始更新方向(比如:你改往东走)
-(void)stopUpdatingHeading; //停止更新方向
CLLocationManagerDelegate是一个委托类。你的应用程序需要使用这个委托类。当用户改变位置的时候,CLLocationManager回调的方法是:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
当用户改变方向的时候,所调用的方法是:
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLLHeading *)newHeading;
当iPhone无法获得当前位置的信息时,所回调的方法是:
-(void)locationManager: (CLLocationManager *)manager didFailLoadWithError:(NSError *)error;


=========================================================================
下面我们来看一个位置类的基本步骤:
一、启动定位服务
CLLocationManager *locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
[locManager startUpdatingLocation];
二、获得位置信息
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation: (CLLocation *)oldLocation
{
NSTimeInterval howRecent = [newLocation.timestamp timeIntervalSinceNow];
if(howRecent < -10) return ; //离上次更新的时间少于10秒
if(newLocation.horizontalAccuracy > 100) return; //精度> 100米
//经度和纬度
double lat = newLocation.coordinate.latitude;
double lon = newLocation.coordinate.longitude;
}
三、获得方向信息(比如往南走)
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
//获得方向
CLLocationDirection heading = newHeading .trueHeading;
}
四、停止定位
[locManager stopUpdatingLocation];
你可以设置你想要的精度和距离过滤:
locManager.desiredAccuracy = kLLocationAccuracyBest;
locManager.distanceFilter = 1000;


MapKit框架:
主要提供了四个功能:显示地图、CLLocation和地址之间的转换、支持在地图上做标记(比如标记北京广场)、把一个位置解析成地址(比如我在水立方,想要知道确切的地址信息)。
MKMapView类:主要是完成下述功能:
-------显示地图,比如:显示北京市的地图;
-------提供多种显示方式,比如标准地图格式,卫星地图等;
-------支持地图的放大缩小;
-------支持在地图上做标记,比如标记广场;
-------在地图上显示手机所在的当前位置。
MKMapView类的属性有:
@property MKCoordinateRegin region; //地图所显示的区域
@property CLLocationCoordinate2D centerCoordinate; //经度和纬度确定的中心位置
@property MKMapView mapType; //地图的显示类型,如:卫星地图
@property NSArray *annotations; //地图上的标记
@property MKUserLocation userLocation; //用户位置
@property id <MKMapViewDelegate>delegate; //委托类


装载地图时的回调方法有:
-(void)mapViewWillStartLocationMap:(MKMapView *) mapView; //开始装载地图
-(void)mapViewDidFinishLocationMap:(MKMapView *)mapView; //结束装载地图
-(void)mapVewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error; //装载失败


当位置发生转变时的回调方法:
-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated; //将要更改
-(void)mapView: (MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated; //已经更改


MKPlacemark、MKUserLocation和MKReverseGeocoder
在地图上做标记是通过MKPlacemark类来完成的。这个类使用(符合)MKAnnotation协议。MKAnnotation包含了多个属性,如:位置(经纬度,CLLocationCoordinate2D类型)、文字标记信息(NSString类型)等。
MKPlacemark保存了位置(经纬度)和地址(字典类)之间的映射。下面是它的初始化方法:
-(void)initWithCoordinate:(CLLocationCoordinate2D *)coordinate addressDictionary:(NSDictionary *)dictionary;
MKUserLocation就是指手机的当前位置,它是MKAnnotation的一个特别案例(因为MKAnnotation可以是地图上的任何标记,而MKUserLocation只是标记了地图上手机所在的当前位置)。这个类包含了多个属性:手机的位置(类型为CLLocation)、位置文字信息(类型为NSString)等。
MKPlacemark保存了位置(经纬度)和地址之间的映射。那么,有没有工具在这两者之间做转换呢?这就是MKRecerseGeocoder.给定一个位置信息,这个类可以返回相应的地址信息。MKReverseGeocoder的初始化方法为:
-(void)initWithCoodinate:(CLLocationCoordinate2D)coordinate;
下面是MKReverseGeocoder常用的一些属性和方法:
@property id <MKReverseGeocoderDelegate>delegate; //委托
-(void)start; //开始转换
-(void)cancel; //取消转换


回调的方法有:
-(void)reverseGeocoder:(MKReverseGeocoder *) geocoded didFindPlacemark:(MKPlacemark *)placemark; //转换成功
-(void)reverseGeocoder : (MKReverseGeocoder *)geocoded didFailWithError:(NSError *)error; //转换失败



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics