百度地图添加自定义图标,以及图标旋转

    xiaoxiao2021-12-14  18

    1、三个属性

    BMKPointAnnotation *customAnnotation;

    BMKAnnotationView *newAnnotationView;

    float direction;

    2、didUpdateUserHeading方法中

    - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation

    {

        [_myMap updateLocationData:userLocation];

        //NSLog(@"heading is %@",userLocation.heading);

        

        if(nil == userLocation || nil == userLocation.heading

           || userLocation.heading.headingAccuracy < 0) {

            return;

        }

        

        CLLocationDirection  theHeading = userLocation.heading.magneticHeading;

        

        direction = theHeading;

        

        if(nil != newAnnotationView) {

            if (direction > 180)

            {

                direction = 360 - direction ;

            }

            else

            {

                direction = 0 - direction ;

            }

            

            newAnnotationView.image = [UIImage imageNamed:@"my_car"];

            newAnnotationView.image = [newAnnotationView.image imageRotatedByDegrees: - direction];

        }

    }

    3、viewForAnnotation

    #pragma mark -显示大头针

    - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation

    {

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

            

            return [(RouteAnnotation*)annotation getRouteAnnotationView:view];

        }

        //此部分为自己添加的部分

        if (annotation == customAnnotation) {

            if (newAnnotationView == nil) {

                newAnnotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

            }

            

            return newAnnotationView;

        }

        

        return nil;

    }

    4、添加自定义的大头针

    - (void)createOverlay{

        CLLocationCoordinate2D annotationCoor = {self.latitudeF, self.longitudeF};

        if (customAnnotation == nil) {

            customAnnotation = [[BMKPointAnnotation alloc] init];

        }

        customAnnotation.coordinate = annotationCoor;

        [_myMap addAnnotation:customAnnotation];

    }

    5、把4的方法添加到

    didUpdateBMKUserLocation:(BMKUserLocation *)userLocation实时调用

    转载请注明原文地址: https://ju.6miu.com/read-964100.html

    最新回复(0)