Google Static Maps URL length limit
Asked Answered
R

10

20

I have 153 markers and need to generate a static map with them, but when put them in the url I get an error like this:

414 Request-Uri Too Large

My url is something like this

"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&......"

Thanks folks!

Redraft answered 25/11, 2010 at 14:23 Comment(0)
B
12

This response came from the Server (google). So you're not allowed to request such long URLs.

See the google api doc for more infos:

Static Map URLs are restricted to 2048 characters in size. In practice, you will probably not have need for URLs longer than this, unless you produce complicated maps with a high number of markers and paths.

Beriosova answered 25/11, 2010 at 14:41 Comment(3)
and what if you need to generate an image with a high number of locations? (ie if I wanted to generate a polygon representing several US counties)Ringdove
I am so tired of people's smart-ass, pointless answers. Why did this answer get upvoted? OBVIOUSLY there are maps out there with more points than can fit in a URL. This is NOT an answer.Vindicate
@Hill Answer and upvote can be faked. You know that most of the thing you can see is fake out instead of real one :DTiossem
P
20

The way i see it is either you reduce precision of your markers in order to gain space in the URL. (i.e. markers=13.72326,-86.13705---->markers=13.73,-86.14) resulting in placing the markers in a grid...
Or you go with a non static api

Porterporterage answered 25/11, 2010 at 15:13 Comment(1)
This is what I ended up doing - rounding in PHP using precision to the 6th decimal really shortened things up for me.Comparison
B
12

This response came from the Server (google). So you're not allowed to request such long URLs.

See the google api doc for more infos:

Static Map URLs are restricted to 2048 characters in size. In practice, you will probably not have need for URLs longer than this, unless you produce complicated maps with a high number of markers and paths.

Beriosova answered 25/11, 2010 at 14:41 Comment(3)
and what if you need to generate an image with a high number of locations? (ie if I wanted to generate a polygon representing several US counties)Ringdove
I am so tired of people's smart-ass, pointless answers. Why did this answer get upvoted? OBVIOUSLY there are maps out there with more points than can fit in a URL. This is NOT an answer.Vindicate
@Hill Answer and upvote can be faked. You know that most of the thing you can see is fake out instead of real one :DTiossem
L
7

Officially you can use 8192 characters

This depends on browser and server, but as a thumb rule 8192 characters should be OK.

Server

Google Maps Static server https://maps.googleapis.com/maps/api/staticmap at 30 July 2019.

Google Static Maps Docs claims that:

Maps Static API URLs are restricted to 8192 characters in size. In practice, you will probably not have need for URLs longer than this, unless you produce complicated maps with a high number of markers and paths. Note, however, that certain characters may be URL-encoded by browsers and/or services before sending them off to the API, resulting in increased character usage.

But in practice, I'm getting errors (413 Payload Too Large) after 15k~ characters mark (30 July 2019):

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

This is based on data got by using fetch() in browser console. I'd assume that Google Static Maps server accepts requests (whole request, not just path) no larger than 16k in size.

Solutions

Reduce Precision

Our company policy is that coordinate data below 6 decimal places (111.32 mm) is useless (considering our market share uses phones and commercial GPS devices).

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)

Encode coordinates

If you have access to encoder you can replace path with an encoded path (use String manipulation instead of URLSearchParams, because searchParams automatically uses encodeURI, which would break your encoded path):

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

Handle Errors

  • Use placeholder image
  • Check if href.length > 8192 before request
  • Trim coordinates
  • Encode path
  • Check if !res.ok && res.status === 413 and display error for user which claims that map is too detailed
  • Use fallback image
Lisabeth answered 30/7, 2019 at 10:14 Comment(1)
Easily the best answer - you mentioned general URL limits, you linked to the GMaps docs on their specific limit, and offered several ways to improve the situation!Berey
E
4

You can encode your Polylines and make them shorter .

Here is the example which can help you how to short your Polylines

I am using the php library to short the length here is the link of library https://github.com/danmandle/encoded-polyline-stitcher

if you use this library

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

Here important point is if you use short url you have to use "enc:" keyword

(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@)

The Library is available for other languages as well if you are not using php .

I hope that it helps others

Esther answered 16/8, 2013 at 15:14 Comment(0)
U
3

Google recently extended the URL limit to 8192, but if you need more than that, you still need to simplify your map or resort to other tricks.

Unwearied answered 9/9, 2016 at 9:58 Comment(0)
T
2

URLs over 2000-ish characters aren't valid. Is your querystring longer than that?

Also see this post

Tollbooth answered 25/11, 2010 at 14:42 Comment(0)
W
1

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));
}
Widgeon answered 4/2, 2015 at 18:7 Comment(0)
F
1

Use osm-static-maps, it's a clone of google static maps but you can send any amount of data to it. https://github.com/jperelli/osm-static-maps

Disclaimer: I'm the author.

Frankfurter answered 28/3, 2020 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.