我为我的网站创建了一个小型实用功能,它完全符合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我不确定我的案例是否正确。那些问题与哪种语言有关? –