IOS地图及定位使用
1.定位
定位使用CoreLocation库,引入CoreLocation/CoreLocation。创建CLLocationManager对象,使用startUpdatingLocation方法开始更新位置信息。
_mgr = [[CLLocationManager alloc] init];
[_mgr requestWhenInUseAuthorization];
_mgr.delegate = self;
[_mgr startUpdatingLocation];
更新成功后,会调用CLLocationManagerDelegate的代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations objectAtIndex:locations.count - ];
NSLog(@"%f,%f\n",location.coordinate.latitude,location.coordinate.longitude);
}
如果要停止更新,调用stopUpdatingLocation即可。
PS1:如果启动时提示是否打开定位,需要调用CLLocationManager对象的requestWhenInUseAuthorization。
PS2:定位用途的提示,在info.plist中添加NSLocationWhenInUseUsageDescription、NSLocationAlwaysUsageDescription添加提示
2.坐标与城市的转换
使用CLGeocoder的
geocodeAddressString将地址转换为CLPlacemark(包含坐标、城市信息等内容),如
CLGeocoder *coder = [[CLGeocoder alloc] init];
[coder geocodeAddressString:@"青岛" completionHandler:^(NSArray *placemarks, NSError *error) {
for (int i = ; i < placemarks.count; i++) {
CLPlacemark *placemark = [placemarks objectAtIndex:i];
NSLog(@"%@,%@",placemark.locality,placemark.country);
}
}];
或reverseGeocodeLocation将坐标转换为CLPlacemark,如
CLGeocoder *geo = [[CLGeocoder alloc] init];
[geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error){
CLPlacemark *placemark = [placemarks objectAtIndex:];
NSLog(placemark.locality);
}];
3.标记(大头针)
大头针要实现MKAnnotation协议,设置其中的coordinate(坐标)、title(标题)、subtitle(副标题)后,用mapview的addAnnotation方法即可添加到地图上,如
@interface MKAnno : NSObject<MKAnnotation> @property (nonatomic,assign) CLLocationCoordinate2D coordinate; // Title and subtitle for use by selection UI.
@property (nonatomic, assign) NSString *title;
@property (nonatomic, assign) NSString *subtitle; @end
添加时使用
MKAnno *anno = [[MKAnno alloc] init];
anno.coordinate = _mapview.centerCoordinate;
anno.title = @"测试位置";
anno.subtitle = @"这就是个测试位置而已";
[_mapview addAnnotation: anno];
调用效果

4.自定义大头针
实现MKMapViewDelegate代理的-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation方法,如
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
NSString *identifier = @"test";
MKPinAnnotationView *view = (MKPinAnnotationView*)[_mapview dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!view)
{
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} //设置右边View为一个自定义Button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[button setTitle:@"我就试试" forState:UIControlStateNormal];
[button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor greenColor]];
view.rightCalloutAccessoryView = button; //设置针头颜色
view.pinColor = MKPinAnnotationColorPurple;
//设置允许弹出框
view.canShowCallout = YES;
//设置掉下动画
view.animatesDrop = YES;
return view;
}
实现方法后,添加大头针效果为

PS:如果返回nil,则按系统默认方法显示,而不是不现实
5.自定义大头针2 - 修改大头针样式
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
NSString *identifier = @"test";
MKPinAnnotationView *view = (MKPinAnnotationView*)[_mapview dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!view)
{
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
view.canShowCallout = YES;
} //覆盖掉默认的
view.annotation = annotation; //设置右边View为一个自定义Button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[button setTitle:@"我就试试" forState:UIControlStateNormal];
[button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor greenColor]];
view.rightCalloutAccessoryView = button; if (arc4random()% > ) {
view.image = [UIImage imageNamed:@"A"];
}
else
{
view.image = [UIImage imageNamed:@"B"];
} return view;
}
注意:不能使用animatesDrop,否则还是大头针
使用效果

IOS地图及定位使用的更多相关文章
- 解决iOS地图持续定位耗电问题
地图位置刷新的代理didUpdateLocations会持续调用,手机非常耗电 但是在实际开发中,有一些APP确实需要用到持续定位的功能,比如:运动类, 导航类, 天气类等等 如何进行持续定位呢?保证 ...
- [OC][地图] 高德地图之定位初探(一)
使用前的说明 高德地图开放平台的iOS定位模块网址-->http://lbs.amap.com/api/ios-location-sdk/summary/ 高德地图有Web端.android平台 ...
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...
- iOS 地图定位及大头针的基本使用
地图 Part1 - 定位及大头针的基本使用 一.MapKit 作用 : 用于地图展示 如大头针,路线,覆盖层展示等(着重界面展示) 使用步骤 导入头文件 #import <MapKit/Map ...
- iOS开发之地图与定位
无论是QQ还是微信的移动客户端都少不了定位功能,之前在微信demo中没有添加定位功能,今天就写个定位的小demo来了解一下定位和地图的东西.地图和定位看上去是挺高大上一东西,其实用法比TableVie ...
- iOS进阶_地图上定位的标志——大头针
一.添加大头针 地图使用的框架是MapKit 大头针走的是MKAnnotation协议 /* 注意:因为是满足协议MKAnnotation,所以没有MKAnnotation的系统大头针类,必须自定义大 ...
- ios开发——实用技术OC篇&地图与定位
地图与定位 11.1 iOS定位服务 11.2 iOS地图 11.3 Web地图 1 iOS定位服务 iOS中有三个定位服务组件: Wifi定位,通过查询一个Wifi路由器的地理位置的信息.比较省电, ...
- iOS中的地图和定位
文章摘自http://www.cnblogs.com/kenshincui/p/4125570.html#location 如有侵权,请联系删除. 概览 现在很多社交.电商.团购应用都引入了地图和定 ...
- iOS UI进阶-4.0 地图与定位
在移动互联网时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边:找餐馆.找酒店.找银行.找电影院 在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能 ...
随机推荐
- access_ok()
access_ok() 函数是用来代替老版本的 verify_area() 函数的.它的作用也是检查用户空间指针是否可用. 函数原型: access_ok (type, addr, size); 变量 ...
- 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据
jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...
- 什么是ajax,ajax原理是什么 ,优缺点是什么
AJAX工作原理及其优缺点 1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页 ...
- 构件图 Component Diagram
构件图是显示代码自身结构的实现级别的图表.构件图由诸如源代码文件.二进制代码文件.可执行文件或动态链接库 (DLL) 这样的构件构成,并通过依赖关系相连接 下面这张图介绍了构件图的基本内容: 下面这张 ...
- android 播放语音文件出现 prepare failed ,不能下载amr文件
amr文件的路径正确,但是android 却不能播放出来. 调试发现时根本就没有下载下来 原因: IIS服务器不允许下载该文件,需要配置MIME 解决方法: 进入IIS目录,配置MIME
- python 基于小顶堆实现随机抽样
起因:之前用蓄水池抽样,算法精简,但直观性很差. 所以这次采用了简单的,为没一个行,赋值一个随机值,然后取 最大的K个作为,随机样本. 基本思路:为每一个行(record,记录,实体) 赋一个rand ...
- c++ std::string 用法
std::string用法总结 在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询.在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(co ...
- Ext学习-前后交互模式介绍
在前后台交互模式的介绍中,实际上就是Store中Proxy相关的内容,比如Ajax提交. 所以详细的文档请参考: Ext学习-基础概念,核心思想介绍 中关于数据模型和MVC结构部分. 作者:sdj ...
- C# memcache
概述 memcache是一套开放源的分布式高速缓存系统.由服务端和客户端组成,以守护程序(监听)方式运行于一个或多个服务器中,随时会接收客户端的连接和操作.memcache主要把数据对象缓存到内存中, ...
- source Insight注册码
source Insight vesion3.50.0058 注 册码SI3US-361500-17409