【问题标题】:Draw route on google maps with animation objective c用动画目标c在谷歌地图上绘制路线
【发布时间】:2018-02-14 10:42:27
【问题描述】:

如何在谷歌地图上绘制带有动画效果的路线, 如何在一段时间后将正确的经纬度上传到本地服务器,这些数据必须准确。

【问题讨论】:

    标签: ios objective-c google-maps routes


    【解决方案1】:

    下面是用动画在两个经纬度之间绘制路线的答案。

    NSInteger animatePathIndex;
    NSTimer *animationTimer;
    
    GMSPath *path;
    GMSMutablePath *animatePath;
    GMSPolyline *animatePolyline;
    
    - (void)fetchPolylineWithOrigin:(CLLocation *)origin destination:(CLLocation *)destination {
        NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
        NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];
        NSString *directionsAPI = @"https://maps.googleapis.com/maps/api/directions/json?";
        NSString *directionsUrlString = [NSString stringWithFormat:@"%@&origin=%@&destination=%@&mode=driving", directionsAPI, originString, destinationString];
        NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString];
    
        NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession] dataTaskWithURL:directionsUrl completionHandler:
         ^(NSData *data, NSURLResponse *response, NSError *error) {
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
             if(error) {
                 //Error in fetching poline
                 return;
             }
    
             NSArray *routesArray = [json objectForKey:@"routes"];
    
             if ([routesArray count] > 0) {
                 NSDictionary *routeDict = [routesArray objectAtIndex:0];
                 NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
                 NSString *points = [routeOverviewPolyline objectForKey:@"points"];
                 GMSPath *path = [GMSPath pathFromEncodedPath:points];
                 [self drawPathWithOutAnimation:path];
             }
         }];
        [fetchDirectionsTask resume];
    }
    
    -(void)drawPathWithAnimation:(GMSPath *)newPath {
        path = newPath;
        animatePolyline = [[GMSPolyline alloc] init];
        animatePath = [[GMSMutablePath alloc] init];
        animatePathIndex = 0;
    
        [animationTimer invalidate];
        animationTimer = nil;
    
        //Whenever timer is triggered it will draw path
        animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.003  target:self selector:@selector(animatePolylinePath) userInfo:nil repeats:YES];
    }
    
    - (void)animatePolylinePath {
    
        //After animaiton completed it will go to else case
        if (animatePathIndex < path.count) {
            [animatePath addCoordinate:[path coordinateAtIndex:animatePathIndex]];
            animatePolyline.path = animatePath;
            animatePolyline.strokeColor = [UIColor redColor];
            animatePolyline.strokeWidth = 2.5;
    
            animatePolyline.geodesic = YES;
            NSArray *styles = @[[GMSStrokeStyle solidColor:PathColor],
                            [GMSStrokeStyle solidColor:PathColor]];
            NSArray *lengths = @[@100000, @50000];
            animatePolyline.spans = GMSStyleSpans(animatePolyline.path, styles, lengths, kGMSLengthRhumb);
            animatePolyline.map = self.mapView;
            animatePathIndex += 1;
        }
        else {
            animatePathIndex = 0;
            [animationTimer invalidate];
            animationTimer = nil;
        }
    }
    

    对于您的另一个问题,请在您要发送位置的时间调用本地服务器 api。

    【讨论】:

    • 你好,这里的 DropMaker 是什么?
    • 忽略 DropMaker,实际上我在我的项目中使用了它。现在我更新了代码
    猜你喜欢
    • 2013-06-23
    • 2011-06-07
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多