Google静态地图网址长度限制

eos*_*s87 17 javascript django google-maps google-maps-api-3 google-maps-static-api

我有153个标记,需要用它们生成一个静态地图,但是当把它们放在网址中时,我得到一个如下错误:

414 Request-Uri Too Large
Run Code Online (Sandbox Code Playgroud)

我的网址是这样的

"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&......"
Run Code Online (Sandbox Code Playgroud)

谢谢大家!

Arg*_*ros 19

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


sch*_*eld 16

此响应来自服务器(谷歌).因此,您不能请求这么长的网址.

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

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

  • 我厌倦了人们的聪明屁股,毫无意义的答案.为什么这个答案得到了提升?显然,有一些地图的点数超过了URL.这不是答案. (5认同)
  • 如果您需要生成具有大量位置的图像怎么办?(即,如果我想生成一个代表美国几个县的多边形) (2认同)

Cro*_*row 6

官方规定可以使用8192个字符

\n\n

这取决于浏览器和服务器,但根据经验,8192 个字符应该没问题。

\n\n

服务器

\n\n

Google 地图静态服务器https://maps.googleapis.com/maps/api/staticmap,2019年 7 月 30

\n\n

Google 静态地图文档声称:

\n\n
\n

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

\n
\n\n

实际上,在 15k~ 字符标记后( 2019 年 7 月 30 日)我收到错误(413 Payload Too Large)

\n\n
\xe2\x95\x94\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa6\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa6\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x97\n\xe2\x95\x91 Browser \xe2\x95\x91 Browser Version \xe2\x95\x91 Valid URL Length \xe2\x95\x91\n\xe2\x95\xa0\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xac\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xac\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa3\n\xe2\x95\x91 Chrome  \xe2\x95\x91 75              \xe2\x95\x91            15489 \xe2\x95\x91\n\xe2\x95\x91 Firefox \xe2\x95\x91 68              \xe2\x95\x91            15761 \xe2\x95\x91\n\xe2\x95\x91 Safari  \xe2\x95\x91 12.1            \xe2\x95\x91            15690 \xe2\x95\x91\n\xe2\x95\x9a\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa9\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa9\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x9d\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

解决方案

\n\n

降低精度

\n\n

我们公司的政策是坐标数据低于小数点后 6 位(111.32 毫米)的坐标数据是无用的(考虑到我们的市场份额使用电话和商用 GPS 设备)。

\n\n

\r\n
\r\n
\xe2\x95\x94\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa6\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa6\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x97\n\xe2\x95\x91 Browser \xe2\x95\x91 Browser Version \xe2\x95\x91 Valid URL Length \xe2\x95\x91\n\xe2\x95\xa0\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xac\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xac\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa3\n\xe2\x95\x91 Chrome  \xe2\x95\x91 75              \xe2\x95\x91            15489 \xe2\x95\x91\n\xe2\x95\x91 Firefox \xe2\x95\x91 68              \xe2\x95\x91            15761 \xe2\x95\x91\n\xe2\x95\x91 Safari  \xe2\x95\x91 12.1            \xe2\x95\x91            15690 \xe2\x95\x91\n\xe2\x95\x9a\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa9\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa9\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x9d\n
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

编码坐标

\n\n

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

\n\n

\r\n
\r\n
const decimalPlaces = 6 // decimal places count\r\nconst decimalPowered = 10 ** decimalPlaces // 1_000_000\r\nconst trim = number => Math.round(number * decimalPowered) / decimalPowered // function that trims number\r\nconst coordinates = [\r\n  [51.838245, -111.834991],\r\n  [51.8331, -111.835],\r\n  [51.831007022306, -111.8232751234],\r\n  [51.838244686875, -111.82327418214]\r\n]\r\nconst trimmedCoordinates = coordinates.map(coordinate => coordinate.map(trim))\r\nconsole.log(trimmedCoordinates)
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

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

\n\n

处理错误

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