//
// ViewController.m
// XMMap
//
// Created by Floating_SH on 15/12/29.
// Copyright © 2015年 SH. All rights reserved.
//
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MyAnnotation.h"
@interface ViewController ()<MKMapViewDelegate>
@property (nonatomic, strong) MKMapView *mapView;
// 定位管理类.
@property (nonatomic, strong) CLLocationManager *manager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 实例化定位管理对象
self.manager = [[CLLocationManager alloc] init];
// 判断当前设备是否支持定位
if ([CLLocationManager locationServicesEnabled] == YES) {
NSLog(@"支持定位");
}else{
NSLog(@"不支持定位功能");
}
// 向系统和用户申请使用权限.
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
// 开始申请
[self.manager requestWhenInUseAuthorization];
}
self.mapView = [[MKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:self.mapView];
// 地图的样式
self.mapView.mapType = MKMapTypeStandard;
// 设置地图显示的中心
CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(40, 116);
[self.mapView setCenterCoordinate:coor];
// 设置地图显示的范围
[self.mapView setRegion:MKCoordinateRegionMake(coor,MKCoordinateSpanMake(3, 3))];
// 跟踪用户
self.mapView.showsUserLocation = YES;
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
// 是否支持地图旋转(默认是开的)
self.mapView.rotateEnabled = NO;
// 设置mapView代理
self.mapView.delegate = self;
// 点击屏幕插一个大头针
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.mapView addGestureRecognizer:tap];
}
- (void)tapAction:(UITapGestureRecognizer *)sender{
// 1.获取用户点击在view的点
CGPoint point = [sender locationInView:self.view];
// 2.将该点转换为mapView上的经纬度坐标
CLLocationCoordinate2D coor = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// 创建一个大头针对象
MyAnnotation *ann = [[MyAnnotation alloc]init];
ann.coordinate = coor;
ann.title = @"拉登在此";
ann.subtitle = @"导弹已定位!";
// 3. 添加大头针的方法
[self.mapView addAnnotation:ann];
// 添加多个大头针
// [self.mapView addAnnotations:array];
}
// 更新用户位置
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSLog(@"更新用户位置成功!");
}
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error{
NSLog(@"更新用户位置失败");
}
// 屏幕显示区域发生变化
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
NSLog(@"屏幕发生了移动");
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"屏幕移动完毕");
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ann"];
if (pinAnnotationView == nil) {
pinAnnotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ann"];
}
// 设置左右视图
pinAnnotationView.leftCalloutAccessoryView = [UIButton buttonWithType:(UIButtonTypeContactAdd)];
pinAnnotationView.rightCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Icon-Small"]];
// 设置大头针的出现效果, 从天而降,只能使用于系统样式的大头针
pinAnnotationView.animatesDrop = YES;
// 设置气泡偏移量
pinAnnotationView.calloutOffset = CGPointMake(0, 0);
// 显示气泡
pinAnnotationView.canShowCallout = YES;
return pinAnnotationView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
----------------------------------下方为大头针类----------------------------------
//
// MyAnnotation.h
// XMMap
//
// Created by Floating_SH on 15/12/29.
// Copyright © 2015年 SH. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
//
// MyAnnotation.m
// XMMap
//
// Created by Floating_SH on 15/12/29.
// Copyright © 2015年 SH. All rights reserved.
//
#import "MyAnnotation.h"
@implementation MyAnnotation
@end