2017-01-23 127 views
0

如何繪製兩條不與道路連接的目的地之間的自定義路線?在iOS的Google地圖中繪製自定義路線

例如,如果來源是薄餅廠和目標是ATI的解決方案我不得不得出這樣的路線,但沒有道路連接這兩個地方。

enter image description here

我想谷歌的折線代碼,但它提供了唯一的道路航點。

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; 

所有幫助表示感謝!

+0

地圖製作擊潰首選的標準方法是在適當的空間道路繞道走,什麼樣定製你想製作的路線? – vaibhav

+0

比薩工廠和ATI解決方案之間沒有連接的道路,只有兩棟建築之間有空間。是否有任何可能的方式來繪製快照中的東西?我的exp地圖中的 –

+0

只會在連接的道路上創建路線,如果您事先提供了靜態的'lat'' long',但也有動態的方式可以在移動[看一看](http:// stackoverflow。 )的COM /問題/ 18422826 /使用-谷歌-地圖如何到磁道-A-用戶 - 地點 - 和 - 顯示最路徑行進-。 – vaibhav

回答

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]); 
        } 
    ]; 
+0

這將繪製道路上的折線。但我需要在非道路區域繪製折線。 –

+0

您可以看到routsArray,並根據您的路線設置自己的自定義經緯度,並使折線 –

+0

路線數組僅提供道路的路標。但我需要非道路的航點。請檢查上面的圖片。 –