关闭

mkmapView的应用

标签: mkmapview应用
66人阅读 评论(0) 收藏 举报
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate,UISearchBarDelegate>
{
    MKPointAnnotation *pint;
    CLGeocoder *clg;
}

@property(nonatomic,strong)MKMapView *mapView;
@property(nonatomic,strong)CLLocationManager *clloca;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    //初始化对象
    self.mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0, 68, self.view.frame.size.width, self.view.frame.size.height)];
    
    clg=[[CLGeocoder alloc] init];
    //标准地图
    self.mapView.mapType=MKMapTypeStandard;
    //当前位置标点类型
    [self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading];
    //是否显示当前位置
    self.mapView.showsUserLocation=YES;
    
    self.mapView.scrollEnabled=YES;
    self.mapView.delegate=self;
    
    //缩放
    self.mapView.zoomEnabled=YES;
    self.mapView.showsScale=YES;
    
    [self.view addSubview:self.mapView];
    
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务不可用!");
        
        return;
    }else{
        
        self.clloca=[[CLLocationManager alloc] init];
        //设置代理
        [self.clloca setDelegate:self];
        //设置精准度
        [self.clloca setDesiredAccuracy:kCLLocationAccuracyBest];
        
        //是否获取授权
        [self.clloca startUpdatingLocation];
        //调取
        [self.clloca requestAlwaysAuthorization];
    }
    
    self.mapView.userInteractionEnabled=YES;
    UILongPressGestureRecognizer *press=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pre:)];
    
    press.minimumPressDuration=0.3;
    [self.mapView addGestureRecognizer:press];
    
    [self addsear];
}

-(void)addsear{
    
    UISearchBar *search=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
    search.showsCancelButton=YES;
    search.showsSearchResultsButton=YES;
    search.delegate=self;
    self.navigationItem.titleView=search;
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    [clg geocodeAddressString:searchBar.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (placemarks.count>0) {
            
            CLPlacemark *place=placemarks[0];
            CLLocation *location=place.location;
            CLLocationCoordinate2D coor=location.coordinate;
            MKCoordinateSpan span;
            span.latitudeDelta=0.01;
            span.longitudeDelta=0.01;
            MKCoordinateRegion region={coor,span};
            [self.mapView setRegion:region animated:YES];
        }
    }];
    searchBar.text=nil;
}


-(void)pre:(UILongPressGestureRecognizer *)sender{
    
    CGPoint point=[sender locationInView:self.mapView];
    CLLocationCoordinate2D coord=[self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    
    CLLocation *location=[[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
    
  //方向解析
    [clg reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *placem=[placemarks objectAtIndex:0];
        
        NSArray *arry=[placem.addressDictionary objectForKey:@"FormattedAddressLines"];
        NSMutableString *str=[[NSMutableString alloc] init];
        
        for (int i=0; i<arry.count; i++) {
            [str appendString:arry[i]];
        }
        
        [self.mapView removeAnnotation:pint];
        pint=[[MKPointAnnotation alloc] init];
        pint.coordinate=coord;
        pint.title=@"放上大头针";
        pint.subtitle=[NSString stringWithFormat:@"%@",str];
        
        [self.mapView addAnnotation:pint];
    }];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    CLLocation *location=[locations firstObject];
    CLLocationCoordinate2D cooldina=location.coordinate;
}


//自定义锚点
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    
    static NSString *str=@"pointid";
    MKAnnotationView *mkan=[mapView dequeueReusableAnnotationViewWithIdentifier:str];
    
    if (mkan==nil) {
        mkan=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:str];
    }
    
    mkan.image=[UIImage imageNamed:@"poi_2"];
    mkan.canShowCallout=YES;
    mkan.selected=YES;
    
    return mkan;
}

+(CLAuthorizationStatus)positionAuthorizationStatus{
    return [CLLocationManager authorizationStatus];
}
+(void)authorizePosition:(CLLocationManager *)manager{
    [manager requestAlwaysAuthorization];
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:67次
    • 积分:4
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档
    阅读排行
    评论排行