2016-07-11 162 views
5

我在我的項目列表中有迷你地圖生成的地方。在這兩點之間,地圖上應該有2個點和着色的道路方向。谷歌靜態地圖與方向

它應該看起來在某種程度上是這樣的:

expected map picture

這應該是靜態的圖像,因爲會有很多這樣的圖片與頁面上的不同方向。但正如我所看到的,Google靜態地圖不允許繪製這樣的圖像。可以有兩個點之間的直接路線,像這樣:

enter image description here

但我需要它的方向......

我決定使用靜態地圖,因爲在我的web應用程序,我收到座標這2點,如果我使用靜態地圖,很容易將它作爲我的PHP模板中的變量。 但是有可能以相同的方式接收方向爲靜態圖像嗎?

我發現用JavaScript API幾個解決方案,但沒有找到如何繪製靜態圖像,因爲我需要......

+2

響應您將需要使用[地圖API(https://developers.google.com/maps/documentation/directions/start),以獲得所有的座標路徑,然後將所有這些座標傳遞給'path'參數中的靜態地圖API。請參閱https://developers.google.com/maps/documentation/static-maps/intro#Paths – duncan

+0

相關問題:[谷歌地圖通過湖泊,河流,山脈的靜態地圖多段線](http://stackoverflow.com/questions/32255380/google-maps-static-map-polyline-passing-lakes-river-mountains) – geocodezip

+0

@geocodezip我不確定我的案例是否正確。那些問題與哪種語言有關? –

回答

9

您可以分兩步做。

  1. 從PHP代碼執行路線請求從步驟1得到的編碼的折線
  2. 使用編碼折線與靜態地圖

例如

https://maps.googleapis.com/maps/api/directions/json?origin=Grodno&destination=Minsk&mode=driving&key=YOUR_API_KEY

這將返回編碼的折線在路線[0] - > overview_polyline->指向

現在使用在靜態地圖的折線:

https://maps.googleapis.com/maps/api/staticmap?size=600x400&path=enc%3AohqfIc_jpCkE%7DCx%40mJdDa%5BbD%7BM%7D%40e%40_MgKiQuVOoFlF%7DVnCnBn%40aDlDkN%7DDwEt%40%7DM%7DB_TjBy%7C%40lEgr%40lMa%60BhSi%7C%40%7COmuAxb%40k%7BGh%5E_%7BFjRor%40%7CaAq%7DC~iAomDle%40i%7BA~d%40ktBbp%40%7DqCvoA%7DjHpm%40uuDzH%7Dm%40sAg%7DB%60Bgy%40%7CHkv%40tTsxAtCgl%40aBoeAwKwaAqG%7B%5CeBc_%40p%40aZx%60%40gcGpNg%7CBGmWa%5CgpFyZolF%7BFgcDyPy%7CEoK_%7BAwm%40%7BqFqZaiBoNsqCuNq%7BHk%60%40crG%7B%5DqkBul%40guC%7BJ%7D%5DaNo%7B%40waA%7DmFsLc_%40_V%7Dh%40icAopBcd%40i_A_w%40mlBwbAiiBmv%40ajDozBibKsZ%7DvAkLm%5DysAk%7DCyr%40i%60BqUkp%40mj%40uoBex%40koAk_E_hG%7B%60Ac%7DAwp%40soAyk%40ogAml%40%7Bg%40qKsNeJw%5DeuA%7D%60Fkm%40czBmK%7Bg%40wCed%40b%40_e%40dT%7BgCzx%40csJrc%40ejFtGi%60CnB_pFhCa%60Gw%40%7Du%40wFwaAmP%7BoA%7Dj%40etBsRm_AiGos%40aCyy%40Lic%40tFohA~NeoCvC_%7CAWm~%40gb%40w~DuLex%40mUk_Ae_%40o_Aol%40qmAgv%40_%7DAaf%40qhAkMcl%40mHwn%40iCuq%40Nqi%40pF%7D%7CE~CyiDmFkgAoUedAcb%40ku%40ma%40cl%40mUko%40sLwr%40mg%40awIoA_aApDe~%40dKytAfw%40kyFtCib%40%7DA%7Bj%40kd%40usBcRgx%40uFwb%40%7BCulAjJmbC~CumAuGwlA_%5Du_C_PqyB%7BI%7DiAwKik%40%7DUcr%40ya%40up%40%7DkB%7DoCoQ%7Da%40aMyf%40an%40wjEimBuwKiYybC%7DLuyBoJ%7DhBuMieAwd%40i%7BB%7B~%40g%60D_Si%5Dsi%40%7Bk%40cPeSuH_T%7DNct%40kNcmC_Gyr%40mq%40_~AkmA%7DkCksByrE_N%7Bc%40oAcs%40%60J%7Bi%40t%7DByaHxNqt%40tGgxA%7CJ%7BkGeJ_aDsQi_HmFwuAmI%7BdA_XijByFgv%40%7DAiwBxDocAdM%7BlAtSmcAfUmaAptAmbGh~AcvGbwBc%7DHff%40shB~Isp%40nQu%7DB%60UsuCbBok%40l%40%7DzAhIwbA~OuaAnYwp%40rYwe%40%7CNke%40zc%40%7BhBrOwRdo%40sf%40xNaTb_%40uy%40ta%40k~%40xTap%40hl%40uiCre%40unHlIi~AlFsc%40rEkk%40aAce%40mL%7DlAwPcyB_GohBzDsqAtMqtA~h%40weDtFkd%40Bi%60%40_XwfEdAag%40dEkM%60%40zAqApJef%40%7BP_o%40sYys%40ai%40yf%40_j%40y_%40oi%40mVi%5EmFqSwAiPtDuQbc%40_nAtZyaAlEkc%40r%40eq%40%7CAo%5BrTwcAtVuz%40vQ%7Dd%40%7CPmb%40xT%7B%5CzZyd%40jG%7BRzL%7Dh%40jr%40ov%40rFiImFqPiD%7BJ&key=YOUR_API_KEY

+0

謝謝,它的工作原理! –

+0

另請參閱http://stackoverflow.com/questions/9015791/show-a-route-on-a-map/9024450#9024450 – miguev

+7

而且您需要在路徑之前添加「enc%3A」。 http://../staticmap?...&path = enc%3A(YOUR_PATH_WITHOUT_BRACKETS) –

8

我爲我的網站創建了一個小型實用功能,它完全符合y的要求您需要:https://github.com/bdupiol/php-google-static-map-directions

給定一個起點,目的地和一系列航點(如果需要),它會爲您提供乾淨的Google靜態地圖網址,並在這些點之間呈現駕駛路徑,並設置適當的標記。

的index.php

<?php 

include "static-map-direction.php"; 

$origin = "45.291002,-0.868131"; 
$destination = "44.683159,-0.405704"; 
$waypoints = array(
    "44.8765065,-0.4444849", 
    "44.8843778,-0.1368667" 
); 

$map_url = getStaticGmapURLForDirection($origin, $destination, $waypoints); 

echo '<img src="'.$map_url.'"/>'; 

靜態映射方向。PHP

<?php 

function getStaticGmapURLForDirection($origin, $destination, $waypoints, $size = "500x500") { 
    $markers = array(); 
    $waypoints_labels = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"); 
    $waypoints_label_iter = 0; 

    $markers[] = "markers=color:green" . urlencode("|") . "label:" . urlencode($waypoints_labels[$waypoints_label_iter++] . '|' . $origin); 
    foreach ($waypoints as $waypoint) { 
     $markers[] = "markers=color:blue" . urlencode("|") . "label:" . urlencode($waypoints_labels[$waypoints_label_iter++] . '|' . $waypoint); 
    } 
    $markers[] = "markers=color:red" . urlencode("|") . "label:" . urlencode($waypoints_labels[$waypoints_label_iter] . '|' . $destination); 

    $url = "https://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&waypoints=" . implode($waypoints, '|'); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POST, false); 
    $result = curl_exec($ch); 
    curl_close($ch); 
    $googleDirection = json_decode($result, true); 

    $polyline = urlencode($googleDirection['routes'][0]['overview_polyline']['points']); 
    $markers = implode($markers, '&'); 

    return "https://maps.googleapis.com/maps/api/staticmap?size=$size&maptype=roadmap&path=enc:$polyline&$markers"; 
} 

結果

+0

不錯,我把它轉換成Ruby代碼:) –

+0

當我在兩個不同的位置嘗試這個時,我看到一個地圖,沒有行會議的地方。我的標記說A和C,這是令人困惑的,因爲我不知道誰有B,但我只有兩個位置。難道是因爲我使用的地址不是longditude和Lat? – Eoin

+0

(也謝謝你,這太神奇了) – Eoin

3

我把Bedu33邏輯在PHP和它在javascript寫生成谷歌地圖的靜態圖像的情況下,有人需要它,像我一樣。此代碼使用從Directions API

var request = directionsDisplay.directions.request; 
var start = request.origin.lat() + ',' + request.origin.lng(); 
var end = request.destination.lat() + ',' + request.destination.lng(); 
var path = directionsDisplay.directions.routes[0].overview_polyline; 
var markers = [];   
var waypoints_labels = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"]; 
var waypoints_label_iter = 0; 
markers.push("markers=color:green|label:" + waypoints_labels[waypoints_label_iter] + '|' + start); 

for(var i=0;i<request.waypoints.length;i++){ 
    //I have waypoints that are not stopovers I dont want to display them 
    if(request.waypoints[i].stopover==true){ 
     markers.push("markers=color:blue|label:" + waypoints_labels[++waypoints_label_iter] + '|' + request.waypoints[i].location.lat() + "," +request.waypoints[i].location.lng()); 
    }   
} 

markers.push("markers=color:red|label:" + waypoints_labels[++waypoints_label_iter] + '|' + end); 

markers = markers.join('&'); 

alert("https://maps.googleapis.com/maps/api/staticmap?size=1000x1000&maptype=roadmap&path=enc:" + path + "&" + markers); 
+0

謝謝。這正是我期待的。 – Biranchi