iOS 硬件 大头针 - 高级 - 修改样式和移动

来源:互联网 发布:郑州软件 编辑:程序博客网 时间:2024/06/06 09:29

#pragma -mark =================================高级 修改大头针样式

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

     类似于tableView的cell 单元格复用

   MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annotation];==pink系统默认的 

    if(annotationView ==nil){

        annotationView = [[
MKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"annotation"];

    }

    annotationView.
annotation= annotation;   //单元格复用

    annotationView.canShowCallout= YES; //显示title subtitle

    annotationView.
pinTintColor= [selfsetRandomColor];   

    annotationView.
animatesDrop= YES;

    annotationView.
draggable= YES;

   
return annotationView;

}

#pragma -mark产生随机颜色

- (
UIColor*)setRandomColor{

   
CGFloat f1 = arc4random_uniform(1001)/1000.0;

   
CGFloat f2 = arc4random_uniform(1001)/1000.0;

   
CGFloat f3 = arc4random_uniform(1001)/1000.0;

   
UIColor *color = [UIColorcolorWithRed:f1green:f2blue:f3alpha:1];

   
return color;
}

#pragma -mark大头针位置状态发生改变时

- (
void)mapView:(MKMapView*)mapView annotationView:(MKAnnotationView*)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState{

   
if(newState == MKAnnotationViewDragStateEnding){

       
CLLocationCoordinate2D coordinate =  view.annotation.coordinate;

       
CLLocation *location = [[CLLocationalloc]initWithLatitude:coordinate.latitudelongitude:coordinate.longitude];

      
__block MyAnnotation *myAnnotation = (MyAnnotation*)view.annotation;

        [
self.geocodereverseGeocodeLocation:locationcompletionHandler:^(NSArray<CLPlacemark*> * _Nullable placemarks,NSError* _Nullable error) {

            [placemarks
enumerateObjectsUsingBlock:^(CLPlacemark* _Nonnull obj, NSUInteger idx,BOOL* _Nonnull stop) {


                myAnnotation.
title= obj.locality;

                myAnnotation.
subtitle= obj.subLocality;
            }];
           
        }];

    }
}
0 0
原创粉丝点击