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大功能 ...
随机推荐
- LINQ.CS
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Zdso ...
- 从零开始学ios开发(十七):Storyboards(上)
在开始这章之前,先做个说明,从这篇开始,我所使用的xcode更新成了最新的版本,版本是4.6.1(4H512),如下: 大家可以打开自己电脑上的App Store,然后搜索xcode,第一个出现的就是 ...
- nodejs小问题:express不是内部或外部命令(转载)
安装express之后发现居然提示express不是内部或外部命令. 工具/原料 Node.js安装包 方法/步骤 1 首先下载Node.js安装包,此处我用的是官方最新的v0.10.27 32位版: ...
- Careercup - Google面试题 - 4557716425015296
2014-05-03 21:57 题目链接 原题: Many sticks with length, every time combine two, the cost is the sum of tw ...
- unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor
eclipse启动项目时,提示超时: 解决方案: 修改 workspace\.metadata\.plugins\org.eclipse.wst.server.core\servers.xml文件. ...
- UML: CIM & PIM
CIM-1:定义业务流程 定义及分析业务流程(Business Process)是为了尽快理清系统范围,以便估算开发成本及时间,可不是为了要改造业务流程.系统分析员千万别误解了此步骤的目的.所以,系统 ...
- java 并发编程
闭锁 一种可以延迟线程的进度直到其到达终止状态.可以用来确保某些活动直到其他活动都完成后才继续执行 例如: 确保某个计算在其需要的所有资源都被初始化了之后才继续执行. 确保某个服务在其他依赖的服务都启 ...
- C#序列化与反序列化(Serialize,Deserialize)实例详解
这篇文章主要介绍了C#序列化与反序列化(Serialize,Deserialize)的方法,实例分析了C#序列化与反序列化的常见技巧,需要的朋友可以参考下 本文实例讲述了C#序列化与反序列化(Seri ...
- css3选择器笔记
通用选择器ul~p{} 为ul之后的所有p标签设置属性 (ul和p为同级元素)ul+p{} 仅为ul之后的p标签设置属性 (ul和p为相邻元素)div>p 为div之后的p标签设置属性{ d ...
- Unity3D研究院之脚本批量打包渠道包研究
原地址:http://www.xuanyusong.com/archives/2418#comments 最近在研究Unity3D脚本批量打包,比如在Android平台下各种不同分辨率和不同内存大小的 ...