2016-06-09 389 views
0

我已經將我的IOS應用程序與谷歌地圖集成在一起。我需要使用谷歌地圖從當前位置到某個(XYZ)目的地繪製最佳路線路徑(較少距離)。我還需要有這種運輸模式(如:駕駛,步行,運輸)IOS:使用谷歌地圖繪製最佳路線路徑

有人可以幫我做到這一點。任何框架得出兩點間線(最佳路徑平局),當我(在每2秒更新路徑)的驅動器點擊

+0

入住這https://developers.google.com/maps/documentation/directions/ – kb920

回答

0

第1步

做一個URL request到谷歌地圖API瀏覽該路徑。

第2步

當您收到JSON file經歷的所有步驟。

步驟3

解碼點對象。


首先獲得所有點的座標這是您的路線就快到了,然後在路徑中添加這些點(緯度和經度這點),你可以將根據該繪製路徑...試試這個:

GMSCameraPosition *position=[GMSCameraPosition cameraWithLatitude:15.5203 longitude:68.8567 zoom:10]; 
_mView =[GMSMapView mapWithFrame:CGRectZero camera:position]; 
_mView.myLocationEnabled=YES; 
GMSMarker *marker=[[GMSMarker alloc]init]; 
marker.position=CLLocationCoordinate2DMake(15.5203, 68.8567); 
marker.icon=[UIImage imageNamed:@"a.png"] ; 
marker.groundAnchor=CGPointMake(0.5,0.5); 
marker.map=_mView; 
GMSMutablePath *path = [GMSMutablePath path]; 
[path addCoordinate:CLLocationCoordinate2DMake(@(15.520).doubleValue,@(68.856).doubleValue)]; 
[path addCoordinate:CLLocationCoordinate2DMake(@(15.7).doubleValue,@(73.8567).doubleValue)]; 

GMSPolyline *rect = [GMSPolyline polylineWithPath:path]; 
rect.strokeWidth = 2.f; 
rect.map = _mView; 
self.view=_mView; 

更新

NSString *urlString = [NSString stringWithFormat: 
         @"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@", 
         @"https://maps.googleapis.com/maps/api/directions/json", 
         mapView.myLocation.coordinate.latitude, 
         mapView.myLocation.coordinate.longitude, 
         destLatitude, 
         destLongitude, 
         @"Enter Google API String Here"]; 
NSURL *directionsURL = [NSURL URLWithString:urlString]; 


ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:directionsURL]; 
[req startSynchronous]; 
NSError *error = [req error]; 
if (!error) { 
    NSString *response = [req responseString]; 
    NSLog(@"%@",response); 
    NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[req responseData] options:NSJSONReadingMutableContainers error:&error]; 
    GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]]; 
    GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path]; 
    singleLine.strokeWidth = 7; 
    singleLine.strokeColor = [UIColor greenColor]; 
    singleLine.map = self.mapView; 
} 
else NSLog(@"%@",[req error]); 
+0

可你給我一個鏈接,這將簡要解釋資訊,請 – Chinnu

+0

檢查我的更新答案兄弟...看到這個視頻也... http://talkminer.com/viewtalk.jsp?videoid=AdV7bCWuDYg&q=&row=4&N=5#.Ui2H1mSAZTE –

+0

謝謝你的建議。但我已經做到了。我需要從我的當前位置到目的地的導航,就像我們在Google地圖中一樣(例如:在10mtr等之後向左轉)。而且我需要每2秒刷新一次地圖。此外,我需要知道什麼邏輯谷歌地圖必須繪製最佳路徑。 – Chinnu

0

希望它幫助你

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) 
                { 
                 if(completionHandler) 
                  completionHandler(nil); 
                 return; 
                } 

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

                GMSPolyline *polyline = nil; 
                if ([routesArray count] > 0) 
                { 
                 dispatch_async(dispatch_get_main_queue(), ^{ 
                 NSDictionary *routeDict = [routesArray objectAtIndex:0]; 
                 NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"]; 
                 NSString *points = [routeOverviewPolyline objectForKey:@"points"]; 
               GMSPolyline *polyline = [GMSPolyline polylineWithPath: [GMSPath pathFromEncodedPath: points]]; 
                 polyline.map = _mView; 
                 polyline.strokeColor = [UIColor colorWithRed:0/255.0 green:4/255.0 blue:255/255.0 alpha:1.0]; 
                 polyline.strokeWidth = 4.0f; 
                  }); 
                 }; 


                if(completionHandler) 
                 completionHandler(polyline); 
               }]; 
    [fetchDirectionsTask resume]; 
} 
+0

你能格式化你的代碼嗎? – Robert

+0

你能解釋你的代碼嗎?只是在這裏傾銷代碼並不能幫助人們學習。 – Robert

+0

它的一個方向API給出經緯度之間的路線,你通過它並在GMSPolyline的幫助下繪製路線 –