码迷,mamicode.com
首页 > 其他好文 > 详细

MapKit 添加大头针

时间:2015-07-17 22:41:38      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"

#import <MapKit/MapKit.h>

#import "MYAnnotation.h"

 

@interface ViewController ()<MKMapViewDelegate>

@property(nonatomic,strong)CLLocationManager *mag;

 

 

@property(nonatomic,strong)CLGeocoder *ceocoder;

@property(nonatomic,strong)MKMapView*mapView;

 

 

@end

 

@implementation ViewController

 

-(MKMapView*)mapView

{

    if (!_mapView) {

        _mapView=[[MKMapView alloc]init];

    }

    return _mapView;

}

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

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

    [self.mag requestAlwaysAuthorization];

    [self.mag requestWhenInUseAuthorization];

    

    //必须要记录要不不会进行用户权限验证

//   CLLocationManager *mags=[[CLLocationManager alloc]init];

//    [mags requestAlwaysAuthorization];

//    [mags requestWhenInUseAuthorization];

 

    

    

    self.mapView.frame=self.view.bounds;

    //self.mapView.visibleMapRect=MKMapRectMake(20, 20, 200, 200);

     [self.view addSubview:self.mapView];

    

    //设置地图的代理

    self.mapView.delegate=self;

    self.mapView.showsUserLocation=YES;

    self.mapView.userTrackingMode=MKUserTrackingModeFollow;

   

    

 

}

//更新用户信息

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

    [self.ceocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {

        CLPlacemark *mark=[placemarks firstObject];

        userLocation.title=mark.name;

        userLocation.subtitle=mark.locality;

    }];

    //设置地图显示的区域

    CLLocationCoordinate2D center=userLocation.location.coordinate;

    //指定经纬度跨度

    MKCoordinateSpan Span=MKCoordinateSpanMake(15, 15);

    //将用户当前位置作为中心

    MKCoordinateRegion region=MKCoordinateRegionMake(center, Span);

    //设置显示区域

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

}

 

//加载大头针

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[MKUserLocation class]]) {

        return nil;

    }

    static NSString*ID=@"anno";

    MKPinAnnotationView *annotationView=(MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];

    if (annotationView==nil) {

        annotationView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:ID];

    }

    //设置大头针的颜色

    annotationView.pinColor=MKPinAnnotationColorPurple;

    //设置大头针动画

    annotationView.animatesDrop=YES;

    

    return annotationView;

}

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint point=[[touches anyObject] locationInView:self.mapView];

    CLLocationCoordinate2D coordinate=[self.mapView convertPoint:point toCoordinateFromView:self.mapView];

    //添加大头针

    MYAnnotation *anno=[[MYAnnotation alloc]init];

    anno.title=@"你想要的";

    anno.subtitle=@"可以用CLGeocoder解析出来位置";

    anno.coordinate=coordinate;

    [self.mapView addAnnotation:anno];

    

} 

@end

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

 

@interface MYAnnotation : NSObject<MKAnnotation>

 

@property(nonatomic,assign)CLLocationCoordinate2D coordinate;

 

@property(nonatomic,copy)NSString*title;

@property(nonatomic,copy)NSString*subtitle;

 

 

@end

@implementation MYAnnotation

 

@end

MapKit 添加大头针

标签:

原文地址:http://www.cnblogs.com/tangranyang/p/4655808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!