Search code examples
c#google-mapsgoogle-maps-static-api

Hundreds of pins in static google map api


This works fine:

static void Main(string[] args)
{
    string latlng = "55.379110,-3.1420898";
    string url = "http://maps.googleapis.com/maps/api/staticmap?center=" + latlng +
       "&zoom=6&size=1000x1000&maptype=satellite&markers=color:blue%7Clabel:S%7C" +
       latlng + "&sensor=false";

    using (WebClient wc = new WebClient())
    {
    wc.DownloadFile(url, @"C:\Bla\img.png");
    }
}

Just wondering, how could I add hundreds of pins and save the map as a png? Surely there is a limit for get request and one can not append too many query string parameters.

PS: The limit is 8192 characters - see https://developers.google.com/maps/documentation/static-maps/intro section URL Size Restriction


Solution

  • You will have to deal with an url size restriction from Google Map Static API. https://developers.google.com/maps/documentation/static-maps/intro#url-size-restriction

    URL Size Restriction Google Static Maps 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. For more information, see Building a Valid URL.


    To Add multiple marker:

    https://developers.google.com/maps/documentation/static-maps/intro

    https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284&key=YOUR_API_KEY