适用于iOS的Google Maps SDK中折线路径的详细程度较低

时间:2018-11-15 08:15:19

标签: ios objective-c google-maps google-maps-sdk-ios

我在我们的应用程序中导入了Google Map SDK,它工作正常,但存在一个问题。 在地图上的贴现率很低,如照片所示: photo 我想这是因为点数很少。 我使用的代码:

- (void)createPolyline {
    KSVMapTasks* mapTasks = [[KSVMapTasks alloc] init];
    CLLocation* locationObject = [[CLLocation alloc] initWithLatitude:self.marker.position.latitude longitude:self.marker.position.longitude];
    [mapTasks fetchPolylineWithOrigin:self.locationManager.location
                          destination:locationObject typeModel:KSVDriving
                    completionHandler:^(NSArray *routesArray) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self createOnMapPolyline:routesArray type:KSVDriving];
                        });
                    }];

    [mapTasks fetchPolylineWithOrigin:self.locationManager.location
                          destination:locationObject typeModel:KSVWalking
                    completionHandler:^(NSArray *routesArray) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self createOnMapPolyline:routesArray type:KSVWalking];
                        });
                    }];
}

- (void)fetchPolylineWithOrigin:(CLLocation *)origin
                    destination:(CLLocation *)destination
                      typeModel:(KSVTypeModel)typeModelParametr
              completionHandler:(void (^_Nonnull)(NSArray *routesArray)) completionHandler
{

    NSString* type = nil;
    switch (typeModelParametr) {
        case  KSVDriving:
            type = @"driving";
            break;

        case KSVWalking:
            type = @"walking";
            break;
    }

    NSString* lang = [Preferences lang];
    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=%@&language=%@&mode=%@&key=%@", directionsAPI, originString, destinationString,lang, type, @"AIzaSyDdTGy9qFpyKLK2UebNR-Hm_bzGgFlAxtI"];
    NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString];

    NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession]
                                                 dataTaskWithURL:directionsUrl
                                                 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
 {
     if(!data && !response)
     {
         if(completionHandler)
             completionHandler(nil);
     } else if (response) {
         NSLog(@"%@", response);
         NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
         NSArray *routesArray = [json objectForKey:@"routes"];
         completionHandler(routesArray);

     } else if (error) {
         if(completionHandler)
             completionHandler(nil);
     }
 }];
    [fetchDirectionsTask resume];
}

这是对路径和其他属性的响应。还有

用于绘制:

- (void) createOnMapPolyline:(NSArray*) routesArray type:(KSVTypeModel) type {
    GMSPolyline *polyline = nil;
    if ([routesArray count] > 0)
    {
        NSDictionary *routeDict = [routesArray objectAtIndex:0];
        NSArray* legs = [routeDict objectForKey:@"legs"];
        NSDictionary* durationDict = [legs[0] objectForKey:@"duration"];
        NSDictionary* distanceDict = [legs[0]  objectForKey:@"distance"];

        NSString* duration = [durationDict objectForKey:@"text"];
        NSString* distance = [distanceDict objectForKey:@"text"];
        NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
        NSString *points = [routeOverviewPolyline objectForKey:@"points"];
        GMSPath *path = [GMSPath pathFromEncodedPath:points];
        polyline = [GMSPolyline polylineWithPath:path];

        .... 

在具有类似代码的Android应用上,其工作正常。 谢谢您的帮助!

0 个答案:

没有答案