2010-10-13 218 views
8

我成功地獲得谷歌靜態地圖顯示2座標之間的路徑。谷歌地圖靜態繪圖「路線」,而不是直線

問題是繪製的路徑只是2點之間的直線。

我讀到爲了能夠在靜態谷歌地圖上的2點之間繪製「路線」,如在道路和城市地理而不是直線之後,我需要添加所有的座標/十字路口路徑。

有沒有人知道一個簡單的解決方案來解決這個問題?

回答

23

您可以用靜態地圖API絕對做到這一點:使用爲DirectionsService

路線:

http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsService

和轉換概述路徑,以適應靜態地圖API的要求:

http://code.google.com/apis/maps/documentation/staticmaps/#Paths

+1

要轉換概覽路徑,您可以使用google.maps.geometry.encoding.encodePath(),然後直接使用它與靜態地圖API – 2012-05-23 15:37:43

+1

是否有一個PHP API而不是JavaScript? – Tharindu 2015-08-28 10:43:26

0

我認爲你不能在staticmap api中使用這個函數。但是,您可以使用Directions和JavaScript API V3。

+0

感謝您的回答。是的,得到方向,然後生成路徑,我知道你可以這樣做,我只是希望有人已經編碼了這個過程:) – Piero 2010-10-13 11:07:11

+0

你絕對可以在靜態地圖上繪製路線 - 只要你有「編碼折線「通過以前的API調用(通過Directions API)。請參閱Static Maps API中的'enc'參數。 – 1owk3y 2017-11-29 05:52:27

1

使用折線可以繪製直線。

Polyline類定義了地圖上連接線段的線性疊加。 Polyline對象由一組LatLng位置組成,並創建一系列線段,以有序的順序連接這些位置。

ü可以看到這裏的例子

https://google-developers.appspot.com/maps/documentation/javascript/examples/polyline-simple

約折線

https://developers.google.com/maps/documentation/javascript/overlays

+0

我認爲這不是一個靜態地圖示例。 – hswner 2015-06-15 23:09:56

-1

我dugged到許多建議和規範,結合大家做一個非常簡單的解決方案,上面的代碼應該爲你工作:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=driving",[startLat floatValue] ,[startLong floatValue], [endLat floatValue], [endLong floatValue]]]; 

NSDictionary *PathInfo;//the json of the result 

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
NSURLResponse *response = nil; 
NSError *error = nil; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
if (!error) { 
    PathInfo = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error]; 
    GMSPath *path =[GMSPath PathInfo[@"routes"][0][@"overview_polyline"][@"points"]]; 
    GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path]; 
    singleLine.strokeWidth = 3; 
    singleLine.strokeColor = [UIColor redColor]; 
    singleLine.map = mapView_; 
}