iOS MapKit+CoreLocation

iOS 系统自带的地图定位功能实现:

环境配置

第一步ios8.0以上需要在plist中设置用户授权,和http白名单

第二步导入 MapKit(地图)+CoreLocation(定位)框架 遵守

CLLocationManagerDelegate,MKMapViewDelegate协议


代码部分

第一部分定位

 _locationM=[[CLLocationManager  alloc]init];

    _locationM.delegate=self;

    _locationM.distanceFilter=1.0f;//设置过滤器的距离

    _locationM.desiredAccuracy=kCLLocationAccuracyBest;//设置精确度

    //[UIDevice currentDevice] 获取当前的机器 拿到机器的系统版本

    if ([[[UIDevice currentDevice]systemVersion] floatValue]>=8.0) {

        [_locationM requestWhenInUseAuthorization];

    }

    [_locationM startUpdatingLocation];//开始定位

定位有2个代理方法

1开始定位成功和位置发生改变时调用

- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray *)locations{

  //数组locations中最后一个元素就是最后更新的位置

    CLLocation *location=[locations lastObject];


  //根据经纬度反向地理编译出地址信息

    //初始化

 CLGeocoder *geocoder=[[CLGeocoder alloc]init];

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        CLPlacemark *placeMark=placemarks[0];

        NSLog(@":%@ :%@ 街道:%@",placeMark.administrativeArea,placeMark.locality,placeMark.thoroughfare);

    }];



}

//定位失败的时候调用的方法

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

    NSLog(@"%@",error);

}


=============================================================================================

第二部分大头针

//这边是自己写的目标经纬度,实际开发中是服务器根据定位点传过来的数据

/这个样式是默认样式,/

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

    [annnotation1 setCoordinate:CLLocationCoordinate2DMake(31.232899, 121.433593)];

    [annnotation1 setTitle:@"大胡子烧烤"];

    [annnotation1 setSubtitle:@"康定路385"];

    [mpview addAnnotation:annnotation1];


大头阵样式2可以点击详情

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

    //判断是不是用户定位

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

        return nil;


    }if ([annotation isKindOfClass:[MKPointAnnotation class]]) {


        MKPinAnnotationView *customPinView = (MKPinAnnotationView*)[mapView

                                                                    dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];


        if (!customPinView) {


            customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation

                                                            reuseIdentifier:@"CustomPinAnnotationView"];


    }


        customPinView.pinTintColor = [UIColor blueColor];

        customPinView.animatesDrop = YES;

        customPinView.canShowCallout = YES;


        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];

        rightButton.backgroundColor = [UIColor grayColor];

        [rightButton setTitle:@"查看详情" forState:UIControlStateNormal];

        customPinView.rightCalloutAccessoryView = rightButton;


        UIImageView *myCustomImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage"]];

        customPinView.leftCalloutAccessoryView = myCustomImage;

        return customPinView;

    }

    return nil;



//点击详情的时候调用

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

}

================================================================================================

第三部分导航

 //目的地经纬度

    CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(31.232899,121.433593);


    //目的地的位置


    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];



    toLocation.name = @"大胡子烧烤";



    NSArray *items = [NSArray arrayWithObjects:toLocation, nil];



    NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };

    //打开苹果自身地图应用,并呈现特定的item


    [MKMapItem openMapsWithItems:items launchOptions:options];



demo地址:https://github.com/missljl/MapKit.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值