IOS中的地图视图MKMapView
来源:互联网 发布:二级c语言上机题库 编辑:程序博客网 时间:2024/05/22 16:53
.m文件
#import "ViewController.h"
#import<MapKit/MapKit.h> //导入框架
@interfaceViewController () <MKMapViewDelegate,CLLocationManagerDelegate>
{
MKMapView * _mapView; //地图,集成了定位功能
CLLocationManager * _locationManager;//位置管理者,可以只使用来定位
}
- (void)initUserInterface; //初始化用户界面
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfinitUserInterface];
}
- (void)initUserInterface
{
// [CLLocationManager locationServicesEnabled]; //检测设备是否支持定位
_locationManager = [[CLLocationManageralloc] init];
[_locationManagerrequestWhenInUseAuthorization]; //请求授权(弹出警告询问是否允许应用程序获取当前位置信息)
[_locationManagerstartUpdatingLocation];
_locationManager.delegate =self;
/*
想要弹出提示框:
必须在plist中添加key -- NSLocationWhenInUseUsageDescription
*/
// [_locationManager requestAlwaysAuthorization];//请求后台运行时获取位置授权
// 添加key---NSLocationAlwaysUsageDescription
_mapView = [[MKMapViewalloc] initWithFrame:self.view.bounds];
_mapView.showsUserLocation =YES; //显示用户位置
// _mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;//用户跟随模式(把地图缩放到定位地点的适合大小)
_mapView.delegate =self;
[self.viewaddSubview:_mapView];
//添加长按手势识别器
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(pressedLongPress:)];
[_mapViewaddGestureRecognizer:longPress];
}
#pragma mark - <CLLocationManagerDelegate>
//更新了用户位置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation * lastLocation = locations.lastObject;
}
#pragma mark - Pressed
- (void)pressedLongPress:(UIGestureRecognizer *)sender
{
//在长按手势开始的时候添加地图标注(系统地图的用户体验)
if (sender.state ==UIGestureRecognizerStateBegan) {
//一.构建标注数据源
MKPointAnnotation * annotation = [[MKPointAnnotationalloc] init];
//1.获取视图上的长按点
CGPoint point = [senderlocationInView:_mapView];
//2.将视图上的点转换为经纬度坐标
CLLocationCoordinate2D coordinate = [_mapViewconvertPoint:point toCoordinateFromView:_mapView];
//3.配置标注数据源的坐标
annotation.coordinate = coordinate;
//4.配置标题
annotation.title =@"坐标获取中..";
// annotation.subtitle = @"call me";
//二.添加这个坐标的标注到地图上
[_mapViewaddAnnotation:annotation];
}
}
#pragma mark - <MKMapViewDelegate>
//更新用户位置后
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//MKUserLocation -> location(CLLocation) -> coordinate(CLCoordinate2D)
// userLocation.location.coordinate.longitude//经度
// userLocation.location.coordinate.latitude//纬度
//设置当前地图显示的区域和范围
[mapView setRegion:MKCoordinateRegionMake(userLocation.location.coordinate,MKCoordinateSpanMake(0.01,0.01)) animated:YES];
}
//自定义标注视图(只要是添加到地图上的标注都会通过这个方法进行标注视图配置)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//MKUserLocation和MKPointAnnotatin都遵循标注协议,可以作为地图标注
if ([annotationisKindOfClass:[MKUserLocationclass]]) {
returnnil;
}
//标注视图的重用
staticNSString * annotationViewID = @"chongyong";
MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[mapViewdequeueReusableAnnotationViewWithIdentifier:annotationViewID];//MKPinAnnotationView是MKAnnotationView的子类 要使用图片配置大头针时使用MKAnnotationView
if (!annotationView) {
annotationView = [[MKPinAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:annotationViewID];
}
//配置大头针颜色
annotationView.pinColor =MKPinAnnotationColorPurple;
//配置大头针的掉落效果
annotationView.animatesDrop =YES;
// annotationView.image = [UIImage imageNamed:@"iconfont-dituzuobiao"]; //MKAnnotationView时可以配置
annotationView.canShowCallout =YES; //是否允许弹框
//配置弹出信息框
UIButton * startButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
startButton.frame =CGRectMake(0,0,60,25);
startButton.layer.cornerRadius =3;
startButton.backgroundColor = [UIColorgrayColor];
[startButton setTitle:@"到这里"forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = startButton;
return annotationView;
}
//当选中了某个标注视图的时候
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
CLLocationCoordinate2D coordiante = ((MKPointAnnotation *)view.annotation).coordinate;
CLLocation * currentLocation = [[CLLocationalloc] initWithLatitude:coordiante.latitudelongitude:coordiante.longitude];
//地理位置逆编码
CLGeocoder * geoCoder = [[CLGeocoderalloc] init];
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks,NSError *error) {
if (placemarks.count >0) {
CLPlacemark * placemark = placemarks[0];
((MKPointAnnotation *)view.annotation).title = placemark.thoroughfare;
((MKPointAnnotation *)view.annotation).subtitle = placemark.subThoroughfare;
}
if (error) {
((MKPointAnnotation *)view.annotation).title = @"获取失败";
}
// NSLog(@"%@",placemarks[0]);
}];
}
//点击信息框中的按钮
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"http://www.baidu.com"]];
}
/***** 总结
//CLLocation包含了坐标、水平高度等的位置
//CLLoactionCoorDinate2D包含经纬度信息,是一个坐标
//MKPointAnnotation 标注,包含坐标及标题、子标题 <MKAnnotation>
//MKUserLocatin 用户位置标注,包含坐标、标题、子标题 <MKAnnotation>
//MKAnnotationView标注视图,包含标注 <MKAnnotation>
//MKPinAnnotationView大头针样式的标注视图
*****/
@end
- IOS中的地图视图MKMapView
- IOS MKMapView 地图视图
- IOS MKMapView 地图视图
- iOS MKMapView 地图视图
- iOS地图MKMapView详析
- iOS MKMapView嵌入地图
- iOS 地图 api MKMapView
- ios-MKMapView苹果地图开发
- iOS 苹果地图 MKMapView总结
- ios利用MKMapView实现简单的地图
- ios利用MKMapView实现简单的地图
- iOS开发 地图MKMapView小Demo
- iOS开发 地图:MKMapView控件的使用
- MKMapView iOS的地图详细应用
- IOS应用开发中的tips - MKMapView之设置地图以用户当前位置为中心
- iOS MKMapView
- iOS-MKMapView
- ios利用MKMapView显示自己当前位置的地图
- 线程第七课,sleep示例代码
- 黑马程序员——面向对象(一)
- 解决用django创建网站时mysql插入中文数据报错的问题
- C++中的虚拟继承
- RabbitMQ,pymongo,bs4的一些记录
- IOS中的地图视图MKMapView
- 错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 蓝桥安慰奶牛
- 将Hadoop 2.6.0源码导入到Eclipse
- error LNK1561: 必须定义入口点
- 浅谈设计模式:迭代器模式(Iterator Pattern)
- POJ1205 DP公式的详细推导思路
- 【Unity3d】学习笔记(4)
- Linux计划任务工具cron用法详解