• IOS开发mapkit学习笔记


           在IOS中使用位置信息时,就需要使用到苹果提供的SDK:mapkit,可以使用不同的API以达到地图信息自定义显示的目的:

    (一) 如果要使用及操作地图,那么首先你需要通过(MKmapview)设定一个用于显示及操作地图内容的环境:

      那么什么是MKmapview呢?

      MKmapview为我们提供了一个操作地图的接口,就如同地图应用直接提供给我们的一样。我们可以用它去显示一些地图信息,或者是用它去操作来自应用的地图内容,也可以将地图定位显示在某一个固定的坐标上,设定你要显示的地图区域大小,当然也可以在地图中添加注释及一些自定义的信息。

        MKMapView* mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];    //初始化一个区域用于显示map信息
         mapView.mapType = MKMapTypeHybrid;     //设置地图显示方式:标准地图,卫星地图,混合地图
         mapView.zoomEnabled = YES;    //设置可缩放环境
         mapView.scrollEnabled = YES;    //设置可滚屏环境
         mapView.showsUserLocation = YES;  //设置是否显示用户位置   
         [self.view add mapView];

    (二)  使用系统提供的Anotation显示用户的当前位置信息:

        注意:此处需要添加corelocation框架;设置delegate方法,并在添加annotation时,若实现了下面的这个方法,那么系统将会调用他,若没实现他,则系统默认返回值为空;

    [- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;]方法,当使用系统提供的annotation时,该方法的返回值为nil;    
         CLLocationCoordinate2D coordinate = {34.7568711,113.663221};   
          MKUserLocation *location = [[MKUserLocationalloc]init];
          location.title = @"beijing";
          location.subtitle = @"subTitle";
          location.coordinate = coordinate;  
          [mapView addAnnotation:location];

    (三)使用setRegin方法,让地图显示某一坐标位置区域

        CLLocationCoordinate2D coordinate = {34.7568711,113.663221};      //将显示位置具体坐标
        MKCoordinateSpan span = {100,100};    //将显示地图区域跨度,即显示地图的精细程度
          MKCoordinateRegion regin;
         regin = MKCoordinateRegionMake(coordinate, span);
         [mapView setRegion:regin];

    (四)使用CLLocationmanager可跟踪当前地理位置实时变化信息 

         CLLocationManager *manager = [[CLLocationManageralloc]init];
          manager.desiredAccuracy = kCLLocationAccuracyBest;
          manager.delegate = self;
          manager.distanceFilter = 100;
          [self.managerstartUpdatingLocation];

      注意:在设置delegate时会调用下面的delegate方法:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation  fromLocation:(CLLocation *)oldLocation
    {
        CLLocationDistance oldDistance = [newLocation distanceFromLocation:oldLocation];
        NSLog(@"old:%f",oldDistance);
    }

    或者

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations  //当调用这个方法的时候,上面的方法会被忽略
    {
    }

     

    (五)设置自定义的annotation标签

      (1)  将Annotation的图标修改为大图标形式可以使用系统提供的方法:

          MKPointAnnotation *anotation = [[MKPointAnnotationalloc]init];
          anotation.coordinate = coordinate;
          anotation.title = @"henan";
          anotation.subtitle = @"qingyun";
          [mapView addAnnotation:anotation];

      

      (2)设置自定义样式的Annotation方式

         在调用这个方法时,系统会自动调用以下方法,因此可以在系统提供的view上自定义各项设置

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        static NSString *identify = @"identify"if ([annotation isKindOfClass:[MKUserLocation class]])
            {
                return nil;
            }
                MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identify];
                if (pinView == nil) {
                    pinView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identify];
                }
                pinView.tintColor = [UIColor greenColor];
                pinView.canShowCallout = YES;        //此处用于显示系统提供的Annotation方式,若想显示完全自定义的Annotation,可将此行注掉
                pinView.image = [UIImage imageNamed:@"icon_nav_start"];         //此行用于显示当前位置的Annotation图像,在自定义模式下必须设置
               
           UIImageView
    *leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon"]]; leftView.frame = CGRectMake(0, 0, 40, 40); pinView.leftCalloutAccessoryView = leftView;
    UIButton
    *button = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure]; button.frame = CGRectMake(0, 0, 30, 20); pinView.rightCalloutAccessoryView = button; return pinView; }

     

      (3)若想要实现Annotation图形的完全自定义,则需要创建一个继承自MKAnnotation的类,并在类中实现如下方法:

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:YES];
        UIView *view  = [[UILabel alloc] initWithFrame:CGRectMake(-40, -40, 100, 40)];
        view.backgroundColor = [UIColororangeColor];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        imageView.image = [UIImage imageNamed:@"icon"];
        [view addSubview:imageView];
        [self addSubview:view];
    }

    (六) 实现对坐标位置的反编码:也就是拿到一个准确的坐标为之后,快速寻找到相应的地理位置

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
         if (nil != error) {
                NSLog(@"error info is %@",error);
                return ;
            }
            for (CLPlacemark *placeMark in placemarks) {
                NSLog(@"%@,%@,%@,%@",placeMark.country,placeMark.administrativeArea,placeMark.locality,placeMark.thoroughfare);
            }
    }];

     

       

  • 相关阅读:
    httpd sshd firewalld 服务后面的d的意思
    js parse_url 引发的
    python的shutil模块
    ubuntu18.04 LTS上安装并使用nvm管理node版本
    【ZIP】打包过滤指定目录和文件
    numpy中的reshape中参数为-1
    机器学习python常用模块
    Python3使运行暂停的方法
    thrift
    一个账号只能在一处登陆,不是单点登陆。
  • 原文地址:https://www.cnblogs.com/yuanjianguo2012/p/3728138.html
Copyright © 2020-2023  润新知