Ios 使用动画objective c在谷歌地图上绘制路线

Ios 使用动画objective c在谷歌地图上绘制路线,ios,objective-c,google-maps,routes,Ios,Objective C,Google Maps,Routes,如何在谷歌地图上用动画效果绘制路线, 如何在一段时间间隔后将正确的纬度和经度上传到本地服务器,这些数据必须准确。下面是用动画绘制两个纬度和经度之间路线的答案 NSInteger animatePathIndex; NSTimer *animationTimer; GMSPath *path; GMSMutablePath *animatePath; GMSPolyline *animatePolyline; - (void)fetchPolylineWithOrigin:(CLLocatio

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

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

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;
    }
}
NSInteger animatePathIndex;
NSTimer*动画定时器;
GMSPath*路径;
GMSMutablePath*动画路径;
GMSPolyline*动画polyline;
-(void)fetchPolylineWithOrigin:(CLLocation*)原点目标:(CLLocation*)目标{
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*方向API=@”https://maps.googleapis.com/maps/api/directions/json?";
NSString*directionsUrlString=[NSString stringWithFormat:@“%@&origin=%@&DETINATION=%@&mode=driving”,directionsAPI,originString,destinationString];
NSURL*directionsUrl=[NSURL URLWithString:directionsUrlString];
NSURLSessionDataTask*FetchDirectionTask=[[NSURLSession sharedSession]dataTaskWithURL:directionsUrl completionHandler:
^(NSData*数据,NSURLResponse*响应,NSError*错误){
NSDictionary*json=[NSJSONSerialization JSONObjectWithData:数据选项:针织错误:&错误];
如果(错误){
//获取poline时出错
返回;
}
NSArray*routesArray=[json objectForKey:@“routes”];
如果([路由阵列计数]>0){
NSDictionary*routeDict=[RouteArray对象索引:0];
NSDictionary*routeOverviewPolyline=[RouteDedit objectForKey:@“概览_polyline]”;
NSString*points=[routeOverviewPolyline objectForKey:@“points”];
GMSPath*path=[GMSPath pathFromEncodedPath:points];
[无动画的自绘制路径:路径];
}
}];
[获取方向任务恢复];
}
-(void)drawPathWithAnimation:(GMSPath*)新建路径{
路径=新路径;
animatePolyline=[[GMSPolyline alloc]init];
animatePath=[[GMSMutablePath alloc]init];
animatePathIndex=0;
[动画计时器失效];
animationTimer=nil;
//无论何时触发定时器,它都将绘制路径
animationTimer=[NSTimer scheduledTimerWithTimeInterval:0.003目标:自选择器:@selector(animatePolylinePath)用户信息:无重复:是];
}
-(void)animatePolylinePath{
//完成动画后,将转到其他案例
if(animatePathIndex

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

您好这里的DropMaker是什么?忽略DropMaker,实际上我在项目中使用了它。现在我更新了代码