很老的線程,但我必須湊齊一些急於解決這個問題非常。如果有其他人遇到同樣的問題,請在此分享。
它需要標記的陣列形式:
$points =
[0] => Array
(
[lat] => 52.1916312
[lng] => -1.7083109
)
[1] => Array
(
[lat] => 50.2681918
[lng] => 2.5616710
)
...
...
...
[500] => Array
(
[lat] => 49.1821968
[lng] => 2.1671056
)
最大URL長度爲2048個字符,從而它首先降低了緯度經度的精度 $ marker_accuracy(4)然後開始從中間去除標記。因爲它 它一次一個
$map_url = make_static_map($points);
function make_static_map($points,$reduce_len=false,$reduce_count=false){
$grp_points = array();
$grps = array();
$url = array();
$max_len = 0;
$width = 640; //max 640 :(
$height = 640; //max 640 :(
$marker_accuracy = 4; //Lat lng to 4 decimal places minimum, 3 would be less accurate
$url[] = 'http://maps.googleapis.com/maps/api/staticmap?';
$url[] = '&size='.$width.'x'.$height.'&scale=2';
$url[] = '&markers=';
if($reduce_count){ //Last resort to shortening this
array_splice($points, ceil(count($points)/2), 1);
}
foreach($points as $i => $point){
if($reduce_len){
$point['lat'] = number_format($point['lat'], $reduce_len, '.', '');
$points[$i]['lat'] = $point['lat'];
$point['lng'] = number_format($point['lng'], $reduce_len, '.', '');
$points[$i]['lng'] = $point['lng'];
}else{
$t_len = max(strlen($point['lat']),strlen($point['lng']));
if($t_len>$max_len){
$max_len = $t_len;
}
}
$grps[] = array($point['lat'],$point['lng']);
}
$grps = remove_duplicate_points($grps);
foreach($grps as $grp){
$grp_points[] = implode(',',$grp);
}
$url[] = implode('|',$grp_points);
$url[] = '&sensor=false';
$url = implode('',$url);
if(strlen($url) > 2048){
// Bugger, too long for google
if($max_len>$marker_accuracy){
// Reduce the length of lat lng decimal places
return(make_static_map($points,$max_len-1,false));
}else{
// Reduce the number of lat lng markers (from center)
return(make_static_map($points,false,true));
}
}else{
return($url);
}
}
function remove_duplicate_points($points){
$points = array_map('serialize', $points);
$points = array_unique($points);
return(array_map('unserialize', $points));
}
以及如果您需要生成具有大量位置的圖像,該怎麼辦? (即如果我想生成一個代表幾個美國縣的多邊形) – PrivateJoker 2015-12-07 17:27:00
我很厭倦人們的聰明屁股,毫無意義的答案。爲什麼這個答案得到upvoted?明顯的是,地圖上有更多的點,而不是可以放在URL中。這不是一個答案。 – Hill 2016-01-07 22:20:44
@Hill答案和upvote可以僞造。你知道你能看到的大部分東西都是虛假的,而不是真正的東西:D – Kasnady 2017-07-11 00:31:15