Google静态地图网址长度限制

时间:2010-11-25 14:23:33

标签: javascript django google-maps google-maps-api-3 google-static-maps

我有153个标记,需要使用它们生成静态地图,但是当它们放入网址时我会收到如下错误:

414 Request-Uri Too Large

我的网址是这样的

"http://maps.google.com/maps/api/staticmap?center=13.00,-85.00&size=600x500&maptype=roadmap&markers=13.305,-86.18636&markers=13.72326,-86.13705&......"

谢谢大家!

10 个答案:

答案 0 :(得分:19)

我看到它的方式是你降低精度以便在URL中获得空间。     (即标记= 13.72326,-86.13705 ----> markers = 13.73,-86.14)导致将标记放置在网格中...
或者你使用非静态api

答案 1 :(得分:16)

此回复来自服务器(谷歌)。因此,您不能请求这么长的网址。

有关更多信息,请参阅google api doc:

  

静态地图网址的大小限制为2048个字符。在实践中,除非您生成具有大量标记和路径的复杂地图,否则您可能不需要长于此的URL。

答案 2 :(得分:5)

答案 3 :(得分:4)

您可以对折线进行编码并缩短它们。

以下示例可帮助您缩短折线

我正在使用php库来缩短长度 这是图书馆https://github.com/danmandle/encoded-polyline-stitcher

的链接

如果您使用此库

(51.838245,-111.834991|51.833179,-111.83503|51.831007022306,-111.8232751234|51.838244686875,-111.82327418214)  =  (atk{HtwqiTt^FpLmhAgl@)

重点是,如果您使用短网址,则必须使用“enc:”关键字

(fillcolor:0xAA000033|color:0xFFFFFF00|51.838245,-111.834991|51.833179,-111.83503|51.831007022306,-111.8232751234|51.838244686875,-111.82327418214)  =   (fillcolor:0xAA000033|color:0xFFFFFF00|enc:atk{HtwqiTt^FpLmhAgl@)

如果您不使用php,该库也可用于其他语言。

我希望它有助于其他人

答案 4 :(得分:3)

Google最近将网址限制扩展为8192,但如果您需要更多,则仍需要简化地图或使用其他tricks

答案 5 :(得分:2)

超过2000个字符的网址无效。你的查询字符串比这长吗?

另见post

答案 6 :(得分:1)

答案 7 :(得分:1)

非常老的线索,但我不得不拼凑一些东西,以赶时间解决这个问题。在这里分享以防其他人有同样的问题。

表格中需要一系列标记:

$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
            )

最大网址长度为2048个字符,因此它首先会降低lat lng的准确度 $ 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));
}

答案 8 :(得分:0)

您可以正式使用8192个字符

这取决于browser和服务器,但是根据经验法则,8192个字符应该可以。

服务器

Google Maps Static服务器https://maps.googleapis.com/maps/api/staticmap,时间为 2019年7月30日

Google Static Maps Docs声称:

  

Maps静态API URL的大小限制为 8192个字符。实际上,除非您生成带有大量标记和路径的复杂地图,否则您可能不需要更长的URL。但是请注意,某些字符在发送给API之前可能会被浏览器和/或服务进行URL编码,从而导致字符使用率增加。

但是在实践中,我在15k〜个字符标记( 2019年7月30日)之后收到错误消息(413有效载荷过大):

╔═════════╦═════════════════╦══════════════════╗
║ Browser ║ Browser Version ║ Valid URL Length ║
╠═════════╬═════════════════╬══════════════════╣
║ Chrome  ║ 75              ║            15489 ║
║ Firefox ║ 68              ║            15761 ║
║ Safari  ║ 12.1            ║            15690 ║
╚═════════╩═════════════════╩══════════════════╝

这基于在浏览器控制台中使用fetch()获得的数据。我假设Google静态地图服务器接受大小不超过16k的请求(整个请求,而不仅仅是路径)。

解决方案

降低精度

我们的公司政策是,6 decimal places(111.32毫米)以下的坐标数据是无用的(考虑到我们的市场份额使用电话和商用GPS设备)。

const decimalPlaces = 6 // decimal places count
const decimalPowered = 10 ** decimalPlaces // 1_000_000
const trim = number => Math.round(number * decimalPowered) / decimalPowered // function that trims number
const coordinates = [
  [51.838245, -111.834991],
  [51.8331, -111.835],
  [51.831007022306, -111.8232751234],
  [51.838244686875, -111.82327418214]
]
const trimmedCoordinates = coordinates.map(coordinate => coordinate.map(trim))
console.log(trimmedCoordinates)

编码坐标

如果您有权使用编码器,则可以用编码路径替换路径(使用String而不是URLSearchParams,因为searchParams自动使用encodeURI,这会破坏编码路径) :

const coordinates = [
  [51.838245, -111.834991],
  [51.8331, -111.835],
  [51.831007022306, -111.8232751234],
  [51.838244686875, -111.82327418214]
].map(([lng, lat]) => ({ lng, lat }))

// path should be MVCArray<LatLng>|Array<LatLng>, if you have Polygon or Polyline, getPath() method should be fine
const path = coordinates.map(coordinate => new google.maps.LatLng(coordinate)) 

const encoded = google.maps.geometry.encoding.encodePath(
  path
);

return (
  url.href +
  `&path=fillcolor:${color}|color:${color}|enc:${encoded}`
);

https://developers.google.com/maps/documentation/maps-static/dev-guide https://developers.google.com/maps/documentation/utilities/polylinealgorithm https://developers.google.com/maps/documentation/javascript/geometry#Encoding

处理错误

  • 使用占位符图像
  • 在请求之前检查是否href.length > 8192
  • 修剪坐标
  • 编码路径
  • 检查是否!res.ok && res.status === 413并向声称地图过于详细的用户显示错误
  • 使用后备图片

答案 9 :(得分:0)

使用osm-static-maps,它是google静态地图的克隆,但您可以向其发送任意数量的数据。 https://github.com/jperelli/osm-static-maps

免责声明:我是作者。