- (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];
}
下面是转载的一些常用信息
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; //转换失败
分享到:
相关推荐
iPhone MKMAPView中根据gps坐标画路径,可以支持google map缩放
地球坐标 火星坐标图吧坐标相互转化,罗列了网上的一些算法,以及百度 高德地图进行转化的api,上次上传的由于加密软件变成了乱码,实在不好意思
当用到CLLocationManager 得到的数据转化为火星坐标, MKMapView不用处理 API 坐标系 百度地图API 百度坐标 腾讯搜搜地图API 火星坐标 搜狐搜狗地图API 搜狗坐标 阿里云地图API 火星坐标 图吧MapBar地图...
MKMapView自定义calloutView,完全自定义布局。。
Swift CLLocationManager演示,并在mapView中显示userLocation。 内部版本:Swift 2.0,Xcode 7.1 尖端 如果您的Xcode项目仅使用Swift,则应设置【构建设置->包含Swift代码的嵌入式内容】 '不' '是的'。 地点 征求...
TSClusterMapView, 用于iOS和OSX的动画集群 MKMapView TSClusterMapView - 带集群的MKMapView动画注释集群MKMapView子类基于 ADClusterMapView 。用法要运行示例项目,克隆 repo,然后从示例目录中运行 pod install ...
MKMapView在中国显示地图和卫星图存在位置上的偏差,因此很多时侯在定位跟踪的时候常常是卫星定位而标准地图显示,因此常发现位置点不居中显示,当然这个可以使用开启用户位置跟踪模式来进行定位。本DEMO也正是利用...
FlyoverKit:在 MKMapView 上实现 360° 俯视图.zip,360° flyover on a MKMapView
MTDirectionsKit, 在MKMapView之上,直接...为什么苹果没有为你提供直接显示在MKMapView之上的方法,强迫你在中 switch,只是为了引导你的用户在某个地方。 我们也是这样,所以我们构建了 MTDirectionsKit 。在你的应
当用到CLLocationManager 得到的数据转化为火星坐标, MKMapView不用处理 API 坐标系 百度地图API 百度坐标 腾讯搜搜地图API 火星坐标 搜狐搜狗地图API 搜狗坐标 阿里云地图API 火星坐标 图吧MapBar地图...
NULL 博文链接:https://chinaxxren.iteye.com/blog/1150309
在一个MKMapView上360° 展示一个高架公路
自定义 callout! 通过手势 添加多个气泡
在 MKMapView 中实现地址选择和导航功能。具有以下特点: 1:支持ios5~7 2:自定义MKAnnotationView 3:支持第三方导航功能、包括高德、百度、苹果、谷歌,以及绘制路线,类似微信发生地址 4:支持滑动地图,选择...
系统地图MKMapView的定位及导航 UISearchController模糊查询点击每个UITableViewCell 在地图上显示大头针 ,点击大头针的右边到这里去,调用系统导航,模糊查询数据源来自百度api 写的有点简单,有点乱
最近使用MKMapView,发现有内存泄露。经查发现属iOS系统 bug,详情我发表的文章:http://blog.csdn.net/rigel_xu/article/details/52146774
8.0以后的GPS定位问题,添加的一些配置信息
locationManager给的坐标为WGS-84,需要转换为中国国测局地理坐标GCJ-02,在项目中使用,误差小于1米
kingpin, 在MKMapView的地图注释集群库中, kingpin一种面向iOS的MKAnnotation集群库。 特性使用 2-d 树,以实现最大性能。无需子类化,使库易于与现有项目集成。安装 CocoaPods要在 Podfile 中获得稳定的发行版,请...
ios 地图开发sample code,画路线图。