mapkit,标记,大头针,自定义,地图圆…

前段时间写过的代码,当时没有注释,结果那会儿又去看,半天才看明白,所以决定标上注释发在博客上,为了以后看它不费事吧

关键字: 苹果地图,标记,大头针,自定义,地图圆圈

- (void)creatMapView

{

    MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height)];

    [self.view addSubview:map];

    map.tag = 1000;

    map.delegate = self;

    self.mapView = map;

    // 判断定位功能是否开启

    if ([CLLocationManager locationServicesEnabled]) {

        CLLocationManager *manager = [[CLLocationManager alloc] init];

        manager.delegate = self;

        self.manager = manager;

        [manager startUpdatingLocation];

        

    }else

    {

        NSLog(@"请开启定位功能!");

    }

}


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

//{

//    if (mapView.tag == 100) {

//        NSLog(@"mapViewUserLoction:%f,%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);

//        [mapView showsUserLocation];

//    }

//}


// 获取通过CLLocationManager获取自己位置

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

{

    CLLocation *location = [locations lastObject];

    [self.manager stopUpdatingLocation];

    [self showUserMapRegioInCenterLoction:location];

    

}


// 显示地图区域

- (void)showUserMapRegioInCenterLoction:(CLLocation *)loction

{

    if (loction) {

        // 拿到CLLocation里的经纬度

        CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(loction.coordinate.latitude, loction.coordinate.longitude);

        // 根据选择范围确定地图显示范围 1m = 0.00001纬度

        MKCoordinateSpan span = MKCoordinateSpanMake(500*0.00001*2, 500*0.00001*2);

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

        self.mapView.showsUserLocation = YES;   // 在地图上显示

        self.loction = loction;

        // 给地图加个圆圈 , radius单位为m

        MKCircle *circle = [MKCircle circleWithCenterCoordinate:coordinate2D radius:500];

        [self.mapView addOverlay:circle];


        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(zoomToAnnotation) userInfo:nil repeats:NO];

    }

}


// 在地图上加入标记信息

- (void)zoomToAnnotation

{

    NSLog(@"=====================%f,%f",self.loction.coordinate.latitude, self.loction.coordinate.longitude);

    if (self.loction) {

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

        annotation.coordinate = CLLocationCoordinate2DMake(self.loction.coordinate.latitude, self.loction.coordinate.longitude);

        annotation.title = @"银都大厦";

        annotation.subtitle = @"尼格科技有限责任公司滴答滴答滴答滴答滴答滴答的的";

        

        [self.mapView addAnnotation:annotation];

        [self.mapView selectAnnotation:annotation animated:YES];

        

        // 如果是多个大头针可以用这个方法

//        [self.mapView addAnnotations:[NSArray array]];

    }

}


#pragma mark -- mapView delegate


// 定义标记视图

- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<<span style="color: #703daa">MKAnnotation>)annotation

{

   

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

        return nil;

    }else if ([annotation isMemberOfClass:[MKPointAnnotation class]])

    {

        static NSString *identifier = @"pin";

        MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (pinAnnotation == nil) {

            pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

        }

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        // 大头针上添加button

        pinAnnotation.rightCalloutAccessoryView = rightButton;

        // 大头针下落动画

        pinAnnotation.animatesDrop = YES;

        // 大头针下落后显示文字

        pinAnnotation.canShowCallout = YES;

        // 拖动大头针

        pinAnnotation.draggable = YES;

        // 设置大头针颜色

        pinAnnotation.pinColor = MKPinAnnotationColorGreen;

        return pinAnnotation;

    }else

    {

        static NSString *identifier = @"imagePin";

        MKAnnotationView *customAnnotation = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (customAnnotation == nil) {

            customAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

        }

        

        // 自定义标记上视图

        customAnnotation.rightCalloutAccessoryView = [[UIView alloc] init];

        

        // 自定义地图标记图片

        customAnnotation.image = [UIImage imageNamed:@""];

        

        customAnnotation.canShowCallout = YES;

        return customAnnotation;

    }

}


// 点击大头针或者地图标记上的button按钮触发

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

{

    NSLog(@"=====datouzhen");

    // 判断是否为大头针上的button

    if([view isKindOfClass:[MKPinAnnotationView class]])

    {

        NSLog(@"===----==datouzhen");

    }

}


// 给圆圈设置必要的属性

- (MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id<<span style="color: #703daa">MKOverlay>)overlay

{

    if ([overlay isKindOfClass:[MKCircle class]]) {     // mapview里找出MKCircle

        

        // 创建MKCircleView

        MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];

        // 设置圆圈里的颜色

        circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.1];

        // 设置圆边线的颜色

        circleView.strokeColor = [[UIColor cyanColor] colorWithAlphaComponent:0.7];

        // 边线宽度

        circleView.lineWidth = 3.0;

        return circleView;

    }else

    {

        return  nil;

    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值