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地图及定位使用的更多相关文章

  1. 解决iOS地图持续定位耗电问题

    地图位置刷新的代理didUpdateLocations会持续调用,手机非常耗电 但是在实际开发中,有一些APP确实需要用到持续定位的功能,比如:运动类, 导航类, 天气类等等 如何进行持续定位呢?保证 ...

  2. [OC][地图] 高德地图之定位初探(一)

    使用前的说明 高德地图开放平台的iOS定位模块网址-->http://lbs.amap.com/api/ios-location-sdk/summary/ 高德地图有Web端.android平台 ...

  3. iOS开发系列--地图与定位

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

  4. iOS 地图定位及大头针的基本使用

    地图 Part1 - 定位及大头针的基本使用 一.MapKit 作用 : 用于地图展示 如大头针,路线,覆盖层展示等(着重界面展示) 使用步骤 导入头文件 #import <MapKit/Map ...

  5. iOS开发之地图与定位

    无论是QQ还是微信的移动客户端都少不了定位功能,之前在微信demo中没有添加定位功能,今天就写个定位的小demo来了解一下定位和地图的东西.地图和定位看上去是挺高大上一东西,其实用法比TableVie ...

  6. iOS进阶_地图上定位的标志——大头针

    一.添加大头针 地图使用的框架是MapKit 大头针走的是MKAnnotation协议 /* 注意:因为是满足协议MKAnnotation,所以没有MKAnnotation的系统大头针类,必须自定义大 ...

  7. ios开发——实用技术OC篇&地图与定位

    地图与定位 11.1 iOS定位服务 11.2 iOS地图 11.3 Web地图 1 iOS定位服务 iOS中有三个定位服务组件: Wifi定位,通过查询一个Wifi路由器的地理位置的信息.比较省电, ...

  8. iOS中的地图和定位

    文章摘自http://www.cnblogs.com/kenshincui/p/4125570.html#location  如有侵权,请联系删除. 概览 现在很多社交.电商.团购应用都引入了地图和定 ...

  9. iOS UI进阶-4.0 地图与定位

    在移动互联网时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边:找餐馆.找酒店.找银行.找电影院   在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能 ...

随机推荐

  1. c语言编程之二叉树

    利用链表建立二叉树,完成前序遍历.中序遍历.后序遍历. 建立二叉树用的是前序遍历建立二叉树: #include<stdio.h> #include<stdlib.h> #inc ...

  2. [原创] zabbix学习之旅六:如何解决zabbix server在内网,而邮件发送服务器在外网的问题

    通过前面的文章,你已经可以快速地搭建一个报警系统,并能正常的收到报警邮件了.不过在很多企业级环境下,邮件发送服务器往往放在外网,而zabbix server放置在内网,在这种情况下,zabbix的报警 ...

  3. C# 该行已经属于另一个表 的解决方法[转]

    该文转自:http://blog.sina.com.cn/s/blog_48e4c3fe0100nzs6.html DataTable dt = new DataTable(); dt = ds.Ta ...

  4. nginx 如何显示真实ip

    nginx做反向代理显示在后台访问的真实ip总是显示127.0.0.1 只要添加如下内容:   proxy_set_header Host $host;  proxy_set_header X-For ...

  5. EXT经验--在调试中通过查看handler的第一个参数的xtype得知该参数信息及该handler的归属

    EXT模拟了OPP的思想,因此很多问题可以像JAVA语音那样去思考它.在实际阅读EXT时,常常需要我们搞清楚某个函数.某个对象的归属.如某个参数变量.方法属于哪个类,如下: 这是我今天在群中发出的问题 ...

  6. 已收录的帝国cms文章被误删除了怎么办?

    我们一直提倡网站要经常备份,但是有时也会遗忘,一不小心被谁删除了那就欲哭无泪了.就像ytkah刚弄了一个站,开了个权限比较高的后台帐号给别人用,居然把两三个栏目都删除了,想发狂啊.刚好又有段时间没备份 ...

  7. Unity3D IOS IPhone添加Admob的方法

    原地址:http://dong2008hong.blog.163.com/blog/static/4696882720140403119293/ 首先阅读官方文档https://developers. ...

  8. 跨线程调用控件之MethodInvoker

    原文:http://www.cnblogs.com/cm8448940/archive/2008/07/10/1240045.html 使用到两个控件,一个按钮button1,一个标签label1. ...

  9. linux源码阅读笔记 数组定义

    在阅读linux源码的过程中遇到了下面的略显奇怪的结构体数组定义. static struct hd_struct{ long start_sect; long nr_sects; }hd[10]={ ...

  10. PHP 开发中的外围资源性能分析(一)

    暂且不讨论「PHP 是不是最好的编程语言」,本文我们将分别分析一下在 PHP 程序的后端外围资源和前端外围资源,它们对整个 PHP Web 应用体验的影响,这往往比语言本身大得多. 首先,后端外围资源 ...