地图定位添加锚点

#import "ViewController.h"

#import

#import

@interface ViewController ()

{

CLGeocoder* geocoder;

}

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

@property (nonatomic,strong)CLLocationManager *locationManager;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"显示当前位置" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

[alter show];

self.locationManager = [[CLLocationManager alloc] init];

// 获取用户的授权

[self.locationManager requestAlwaysAuthorization];

[self.locationManager setActivityType:CLActivityTypeOther];

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 定位精度

self.locationManager.delegate = self;

self.mapView.delegate = self;

[self.locationManager startUpdatingLocation];

// 创建一个手势处理器,用于检测、处理长按手势

UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

// 为该控件添加手势处理器

[self.view addGestureRecognizer:gesture];

}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

// 获取当前位置

CLLocation *currentLoc = [locations lastObject];

// 创建MKPointAnnotation对象——代表一个锚点

MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];

annotation.title = @"当前位置";

// 获取当前经纬度

CLLocationCoordinate2D center = currentLoc.coordinate;

annotation.coordinate = center;

// 添加锚点

[self.mapView addAnnotation:annotation];

// 设置地图显示的范围,

MKCoordinateSpan span;

// 地图显示范围越小,细节越清楚

span.latitudeDelta = 0.01;

span.longitudeDelta =0.01;

// 创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。

MKCoordinateRegion region = {center,span};

// 设置当前地图的显示中心和显示范围

[self.mapView setRegion:region animated:YES];

}

-(void)longPress:(UILongPressGestureRecognizer *)sender

{

NSLog(@"1");

CGPoint point = [sender locationInView:_mapView];

CLLocationCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:_mapView];

//    CLLocation *location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];

MKPointAnnotation *annocation = [[MKPointAnnotation alloc] init];

annocation.coordinate = coord;

annocation.title = @"我的锚点";

[_mapView addAnnotation:annocation];

}

#if 1

// MKMapViewDelegate协议中的方法,该方法的返回值可用于定制锚点控件的外观- (MKAnnotationView *) mapView:(MKMapView *)mapView            viewForAnnotation:(id) annotation

{

static NSString* annoId = @"fkAnno";

// 获取可重用的锚点控件

MKAnnotationView* annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoId];

// 如果可重用的锚点控件不存在,创建新的可重用锚点控件

if (!annoView)

{

annoView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoId];

/*

如果不想改变锚点控件的图片,只想改变颜色,则可创建MKPinAnnotationView实例

再修改MKPinAnnotationView对象的pinColor属性即可。

*/

}

// 为锚点控件设置图片

annoView.image = [UIImage imageNamed:@"pos.gif"];

// 设置该锚点控件是否可显示气泡信息

annoView.canShowCallout = YES;

// 可通过锚点控件的rightCalloutAccessoryView、leftCalloutAccessoryView设置附加控件

return annoView;

}

#endif

你可能感兴趣的:(地图定位添加锚点)