我爲我的網站創建了一個小型實用功能,它完全符合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";
}
結果
響應您將需要使用[地圖API(https://developers.google.com/maps/documentation/directions/start),以獲得所有的座標路徑,然後將所有這些座標傳遞給'path'參數中的靜態地圖API。請參閱https://developers.google.com/maps/documentation/static-maps/intro#Paths – duncan
相關問題:[谷歌地圖通過湖泊,河流,山脈的靜態地圖多段線](http://stackoverflow.com/questions/32255380/google-maps-static-map-polyline-passing-lakes-river-mountains) – geocodezip
@geocodezip我不確定我的案例是否正確。那些問題與哪種語言有關? –