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大功能 ...
随机推荐
- Ubuntu修改密码长度太短或太简单解决
在安装 Ubuntu 的时候建立的帐户 sai,想把密码改成两个字母aa,方便输入. 运行终端 sai@xmomx:~$ passwd sai更改 sai 的密码.(当前)UNIX 密码: xx输入新 ...
- Struts 2简单配置分析
要配置Struts 2,首先先要有Struts 2的Jar包,可以去Struts的官网下载(http://struts.apache.org/),这里有3个GA版本可以选择下载,我选择的是最新的2.2 ...
- How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?
The more general extension of ViewPager would bet to create a "SetPagingEnabled" method so ...
- Interview-Harry Potter walk through matrix.
假设你是harry potter,在grid的左上角,你现在要走到右下角,grid中有正数也有负数,遇到正数表示你的strength增加那么多,遇到负数表示strength减少那么多,在任何时刻如果你 ...
- 2006: [NOI2010]超级钢琴 - BZOJ
Description小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为 ...
- 【转载】Hadoop历史服务器详解
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:过往记忆(http://www.iteblog.com/) 原文地址: ...
- 【HDOJ】【2089】不要62
数位DP cxlove基础数位DP第一题 用容斥把所有的不吉利数字去掉就得到吉利数字的数量= =(满足区间减法) //HDOJ 2089 #include<cmath> #include& ...
- Android SDK下载地址
原地址:http://lameck.blog.163.com/blog/static/38811374201262111309677/ Android SDK.ADT.tools等官方下载地址(201 ...
- Calling Lua From a C Program
Introduction From a running C program, you can call a Lua script. The C program can pass arguments t ...
- hdu 1863 畅通工程(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...