美文网首页
iOS8 苹果自带地图的使用

iOS8 苹果自带地图的使用

作者: 墨香茉香 | 来源:发表于2017-10-30 10:47 被阅读0次

    iOS8的发布,苹果系统自带的地图,带来几个新的方法,以下给大家介绍以下使用流程:

    一、在项目中添加以下及格框架:

    #import  <CoreLocation/CoreLocation.h>

    #import  <MapKit/MapKit.h>


    二、创建对象:

    //CLLocation事件通过CLLocationManager实例的委托方法产生。

    //CLLocation对象不仅封装了地理信息,而且还有速度、高度、方向和精确度等信息

    CLLocation*location;//地理位置对象

    /*

    CLLocationManager只有两个委托方法。第一个是locationManager:didUpdateTo Location:fromLocation:,

    当位置管理器更新一个新的位置的时候调用。之前的位置和新的位置都会传递,除非这是第一个位置事件,在那种情况下fromLocation:将会是nil。

    第二个委托方法locationManager:didFail WithError:,用于处理位置管理器试图获取一个位置值的时候所发生的任何错误。

    在这种情况下,可以通过一个stopUpdatingLocation调用来停止位置管理器,以节省电池电量。

    */

    @property(nonatomic,strong)CLLocationManager*locationManager;//定位管理

    @property(nonatomic,assign)floatlatitude,longitude;//经纬度

    @property(weak,nonatomic)IBOutletMKMapView*mapView;//地图

    三、在项目的info.plist中添加两个字符串:NSLocationAlwaysUsageDescription;NSLocationWhenInUseUsageDescription(注意别写错,否则代理方法不执行,无法定位

    四:在viewDidLoad实现locationManager属性的初始化

    - (void)viewDidLoad {

    [superviewDidLoad];

    span=MKCoordinateSpanMake(0.04,0.04);

    //给CLLocationManager对象分配内存

    self.locationManager=[[CLLocationManageralloc]init];

    //设置起代理

    self.locationManager.delegate=self;

    //设置定位的精确度(默认值)

    self.locationManager.desiredAccuracy=kCLDistanceFilterNone;

    //开始定位

    [self.locationManagerstartUpdatingLocation];

    //设置地图的代理

    self.mapView.delegate=self;

    //设置地图索要标识的为用户的位置

    self.mapView.showsUserLocation=YES;

    //添加长按手势(长按添加一个大头针)

    UILongPressGestureRecognizer* tap = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapGestrueAction:)];

    [self.mapViewaddGestureRecognizer:tap];

    }

    #pragma mark实现CLLocationManager代理方法

    //此方法会在用户授权状态改变时调用(iOS8才会调用)

    -(void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

    {

    switch(status)

    {

    casekCLAuthorizationStatusNotDetermined:

    if([self.locationManagerrespondsToSelector:@selector(requestAlwaysAuthorization)])

    {

    [self.locationManagerrequestAlwaysAuthorization];//只要用户授权了就可使用定位

    }

    break;

    }

    }

    //更新地理位置会执行的代理方法

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

    {

    //获取地理位置

    location=locations[0];

    //根据经纬度解析地理位置

    CLGeocoder*geocoder=[[CLGeocoderalloc]init];

    [geocoderreverseGeocodeLocation:locationcompletionHandler:^(NSArray*placemarks,NSError*error) {

    CLPlacemark*mark=placemarks[0];//地理信息对象(包括国家,省,市,县区等等)

    NSString*title=[NSStringstringWithFormat:@"%@-%@-%@-%@",mark.subLocality,mark.thoroughfare,mark.subThoroughfare,mark.name];

    NSLog(@"----%@----",title);

    self.title=title;

    //经度纬度对象

    CLLocationCoordinate2Dcoodinate=location.coordinate;

    //现实地图出现的可视范围

    self.mapView.region=MKCoordinateRegionMake(coodinate,span);

    [managerstopUpdatingLocation];//结束定位

    }];

    }

    //获取地理星系失败调用的方法

    -(void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error

    {

    NSLog(@"error:%@",error);

    }

    #pragma end mark

    #pragma mark实现地图代理,插图

    //返回插图的样式

    -(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation

    {

    staticNSString* u=@"user_id";//自己

    staticNSString* o=@"other_id";//别人

    staticNSString* reuser_id ;//= user_id;

    reuser_id = o;

    if([annotation.titleisEqualToString:@"我在这"])

    {

    reuser_id = u;

    }

    MKAnnotationView* av = [mapViewdequeueReusableAnnotationViewWithIdentifier:reuser_id];

    if(!av)

    {

    av = [[MKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:reuser_id];

    av.canShowCallout=YES;

    }

    //设置自定义大头针的样式

    UIImageView* imgv = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"head.png"]];

    imgv.center=av.center;

    imgv.layer.cornerRadius=imgv.frame.size.width/2.0;//图片的形状为圆形

    imgv.layer.masksToBounds=YES;

    [avaddSubview:imgv];

    returnav;

    }

    //地图用户地理位置的更新时调用的代理方法

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

    {

    location=userLocation.location;//用户当前的地理位置

    userLocation.title=@"昵称";

    userLocation.subtitle=self.title;

    if(!userShowed)

    {

    [mapViewsetRegion:MKCoordinateRegionMake(userLocation.coordinate,MKCoordinateSpanMake(.1,.1))animated:YES];

    userShowed=YES;//定位成功改变为YES

    }

    }

    //在地图上添加新的大头针调用的方法- (void)mapView:(MKMapView*)mv didAddAnnotationViews:(NSArray*)views

    {

    for(MKAnnotationView*annoViewinviews)//先获取当前点中的位置是否已经存在定位

    {

    UIImageView* imgv = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"head.png"]];

    imgv.layer.cornerRadius=imgv.frame.size.width/2.0;

    imgv.layer.masksToBounds=YES;

    annoView.leftCalloutAccessoryView=imgv;

    imgv.frame=CGRectMake(0,0,50,50);

    PlaceMark*anno = annoView.annotation;

    [mvselectAnnotation:annoanimated:YES];

    }

    }


    以上是苹果系统自带的地图的简单应用。

    相关文章

      网友评论

          本文标题:iOS8 苹果自带地图的使用

          本文链接:https://www.haomeiwen.com/subject/jylppxtx.html