在iOS中的谷歌地图中绘制自定义路线

时间:2017-01-23 07:30:17

标签: ios objective-c google-maps

如何在两个未与道路连接的目的地之间绘制自定义路线?

例如,如果来源是The Pizza Factory而目的地是ATI解决方案我必须像这样绘制路线但是没有道路连接这两个地方

enter image description here

我尝试了Google的Polylines代码,但它只在道路上提供了航点。

NSString *urlString = [NSString stringWithFormat:@"%@?origin=%@,%@&destination=%f,%f&sensor=false&waypoints=optimize:true&mode=driving", @"https://maps.googleapis.com/maps/api/directions/json", getmycurrlat, getmycurrlong, LATI, LONGI];
    NSLog(@"my driving api URL --- %@", urlString);
    NSLog(@"you clicked on button %ld", (long)sender.tag);
    NSURL* url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@", urlString]];
    NSURLResponse* res;
    NSError* err;
    NSData* data = [NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc] initWithURL:url] returningResponse:&res error:&err];
    if (data == nil) {
        return;
    }

    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSDictionary* routes = [dic objectForKey:@"routes"][0];
    NSDictionary* route = [routes objectForKey:@"overview_polyline"];
    NSString* overview_route = [route objectForKey:@"points"];

    GMSPath* path = [GMSPath pathFromEncodedPath:overview_route];
    GMSPolyline* polyline = [GMSPolyline polylineWithPath:path];
    polyline.strokeWidth = 5.f;
    polyline.map = self.mapView;

感谢所有帮助!!

1 个答案:

答案 0 :(得分:0)

尝试这对我有用,希望它能帮到你,我正在使用AFNetworking来获取数据

    NSString *url1 = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?&origin=%@&destination=%@&mode=driving",[NSString stringWithFormat:@"%f,%f",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude],pickLoc];
    AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
    [operationManager POST:url1
                parameters: nil
                   success:^(AFHTTPRequestOperation *operation, id responseObject) {
                       // NSLog(@"JSON: %@", [responseObject description]);

                       NSArray *routesArray = [responseObject objectForKey:@"routes"];

                       GMSPolyline *polyline = nil;

                       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];
                           polyline = [GMSPolyline polylineWithPath:path];
                           polyline.strokeWidth = 3;
                           polyline.strokeColor = [UIColor colorWithRed:0.9607 green:0.0358 blue:0.1529 alpha:0.6];
                           polyline.map = self.viewMapView;
                       }
                   }
                   failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                       NSLog(@"Error: %@", [error description]);
                   }
     ];