mkmapView的应用

#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];
}
阅读更多
文章标签: mkmapview 应用
想对作者说点什么? 我来说一句

MKMapView 同时显示多气泡

2016年12月09日 1.3MB 下载

地图导航与定位

2012年04月08日 944KB 下载

MKMapView自定义calloutView

2013年07月15日 76KB 下载

IOS MKMapView

2013年06月03日 185KB 下载

google Map开发

2011年06月27日 1.06MB 下载

iPhone MKMAPView画路径

2011年05月24日 31KB 下载

ios学习之谷歌地图

2012年10月08日 1.89MB 下载

ios MapView 绘制地图路径

2013年08月05日 64KB 下载

GPS定位MKMapView,CoreLocation

2015年05月13日 31KB 下载

没有更多推荐了,返回首页

关闭
关闭