IOS 地图定位,标注地图,获取经纬度:
一、使用MKMapView 定位
二、在地图上通过经纬度标注(大头针)
//
// ViewController.m// MKMapView的使用及定位自己当前的位置
//
// Created by 爱敲代码的小明 on 15/12/26.
// Copyright © 2015年 爱敲代码的小明. All rights reserved.
//
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
{
MKMapView *mapView;
CLGeocoder *geocoder;
}
@end
@implementation ViewController
//创建提示框是否提示
- (void)viewDidAppear:(BOOL)animated
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否显示用户当前位置" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定 " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
mapView.showsUserLocation = 1;
}];
[alert addAction:action];
[alert addAction:action2];
[self presentViewController:alert animated:1 completion:^{
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
//设置缩放 旋转 滚动
mapView.zoomEnabled = 1;
mapView.scrollEnabled = 1;
mapView.rotateEnabled = 1;
<span style="color:#FF0000;">//地图模式,默认是standard模式,还有卫星模式satellite和杂交模式Hybrid</span> mapView.mapType = MKMapTypeStandard;
//设置代理
// mapView.centerCoordinate = CLLocationCoordinate2DMake(37.32, -122.03);
[self.view addSubview:mapView];
//做到这里,尤为重要,不要忘记设置
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(PressG:)];
[self.view addGestureRecognizer:longP];
geocoder = [[CLGeocoder alloc] init];
[self locationLatitude:23 and:112];
}
//是地图上大头针模型,有title和subtitle以及location信息。该方法调用频繁
- (void)locationLatitude:(CGFloat)latitude and:(CGFloat)longitude
{
CLLocationCoordinate2D center = {latitude, longitude};
MKCoordinateSpan span;
span.latitudeDelta = 0.05;
span.longitudeDelta = 0.05;
MKCoordinateRegion region = {center, span};
[mapView setRegion:region animated:1];
MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
ann.title = @"你的位置";
ann.subtitle = @"你的位置";
ann.coordinate = center;
[mapView addAnnotation:ann];
}
<span style="color:#FF0000;">//可以打印经纬度的跨度,用来测试当前视图下地经纬度跨度是多少,然后用于上面的方法中</span>- (void)PressG:(UILongPressGestureRecognizer *)sender {
// CGPoint point = [sender locationInView:mapView];
// CLLocationCoordinate2D locationn = [mapView convertPoint:point toCoordinateFromView:mapView];
//
// CLLocation *cllo = [[CLLocation alloc] initWithLatitude:locationn.latitude longitude:locationn.longitude];
// [geocoder reverseGeocodeLocation:cllo completionHandler:^(NSArray<CLPlace包容的白昼 *> * _Nullable place包容的白昼s, NSError * _Nullable error) {
// if (place包容的白昼s.count > 0 && error == nil) {
// CLPlace包容的白昼 *placema = place包容的白昼s[0];
// NSArray *arr = placema.addressDictionary[@"FormattedAddressLines"];
//
// NSMutableString *str = [[NSMutableString alloc] init];
// for (int i = 0; i < arr.count; i++) {
// [str appendString:arr[i]];
// }
//
// MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
// anno.title = placema.name;
// anno.subtitle = str;
// anno.coordinate = locationn;
//
// [mapView addAnnotation:anno];
// }
// }];
CGPoint pos = [sender locationInView:mapView];
// 将长按点的坐标转换为经度、维度值
CLLocationCoordinate2D center = [mapView convertPoint:pos toCoordinateFromView:mapView];
// 将经度、维度值包装为CLLocation对象
CLLocation *location = [[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude];
// 根据经、纬度反向解析地址
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlace包容的白昼 *> * _Nullable place包容的白昼s, NSError * _Nullable error) {
if ( error == nil)
{
// 获取解析得到的第一个地址信息
CLPlace包容的白昼* place包容的白昼 = [place包容的白昼s objectAtIndex:0];
// 获取地址信息中的FormattedAddressLines对应的详细地址
NSArray* addrArray = place包容的白昼
.addressDictionary[@"FormattedAddressLines"];
// 将详细地址拼接成一个字符串
NSMutableString* address = [[NSMutableString alloc] init];
for(int i = 0 ; i < addrArray.count ; i ++)
{
[address appendString:addrArray[i]];
}
// 创建MKPointAnnotation对象——代表一个锚点
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.title = place包容的白昼.name;
annotation.subtitle = address;
annotation.coordinate = center;
// 添加锚点
[mapView addAnnotation:annotation];
}
}];
}
- (MKAnnotationView *) mapView:(MKMapView *)mmapView
viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString* annoId = @"fkAnno";
// 获取可重用的锚点控件
MKAnnotationView* annoView = [mmapView
dequeueReusableAnnotationViewWithIdentifier:annoId];
// 如果可重用的锚点控件不存在,创建新的可重用锚点控件
if (!annoView)
{
annoView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoId];
/*
如果不想改变锚点控件的图片,只想改变颜色,则可创建MKPinAnnotationView实例
再修改MKPinAnnotationView对象的pinColor属性即可。
*/
}
// 为锚点控件设置图片
annoView.image = [UIImage imageNamed:@"pos.gif"];
// 设置该锚点控件是否可显示气泡信息
annoView.canShowCallout = YES;
// 定义一个按钮,用于为锚点控件设置附加控件
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// 为按钮绑定事件处理方法
[button addTarget:self action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// 可通过锚点控件的rightCalloutAccessoryView、leftCalloutAccessoryView设置附加控件
annoView.rightCalloutAccessoryView = button;
return annoView;
}
//如果是iOS8模拟器,会出现如下报错:
//Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end