How to get animated polyline route in GMSMapView, so that it move along with map when map is moved?

后端 未结 6 1384
独厮守ぢ
独厮守ぢ 2020-12-02 13:37

I have created animated polyline like CAShapeLayer by following code, I have added CAShapeLayer as sublayer to GMSMapiew but, if I move the map the layer won\'t moves. wher

6条回答
  •  星月不相逢
    2020-12-02 14:23

    I am create GMSPath animation with path coordinate

    Objective C

    interface

    @interface MapWithTracking () 
    
    @property (weak, nonatomic) IBOutlet GMSMapView *mapView;
    
    @property (nonatomic,strong) GMSMutablePath *path2;
    
    @property (nonatomic,strong)NSMutableArray *arrayPolylineGreen;
    
    @property (nonatomic,strong) GMSPolyline *polylineBlue;
    
    @property (nonatomic,strong) GMSPolyline *polylineGreen;
    
    @end
    

    implementation

    -(void)viewDidLoad {
        _arrayPolylineGreen = [[NSMutableArray alloc] init];
        _path2 = [[GMSMutablePath alloc]init];
    }
    

    Get a GMSPath and create a blue polyline.

    -(void)createBluePolyline(GMSPath *path) {
    
           // Here create a blue poly line
          _polylineBlue = [GMSPolyline polylineWithPath:path];
          _polylineBlue.strokeColor = [UIColor blueColor];
          _polylineBlue.strokeWidth = 3;
          _polylineBlue.map = _mapView;
    
           // animate green path with timer
           [NSTimer scheduledTimerWithTimeInterval:0.003 repeats:true block:^(NSTimer * _Nonnull timer) {
                 [self animate:path];
           }];
    
    }
    

    Animate a Green Polyline

    Adding coordinate to path 2 and assign to map

    -(void)animate:(GMSPath *)path {
    
        dispatch_async(dispatch_get_main_queue(), ^{
            if (i < path.count) {
                [_path2 addCoordinate:[path coordinateAtIndex:i]];
                _polylineGreen = [GMSPolyline polylineWithPath:_path2];
                _polylineGreen.strokeColor = [UIColor greenColor];
                _polylineGreen.strokeWidth = 3;
                _polylineGreen.map = _mapView;
                [arrayPolylineGreen addObject:_polylineGreen];
                i++;
            }
            else {
                i = 0;
                _path2 = [[GMSMutablePath alloc] init];
    
                for (GMSPolyline *line in arrayPolylineGreen) {
                    line.map = nil;
                }
    
            }
        });
    }
    

提交回复
热议问题