Very old thread but I had to cobble together something to tackle this very problem in a hurry. Sharing here in case anyone else had the same problem.
It takes an array of markers in the form:
$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
)
Max url length is 2048 chars so it first reduces accuracy of the lat lng to
$marker_accuracy (4) then starts removing markers from the middle.
Removing markers from the middle could be improved a lot as it does
it one at a time
$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));
}