views:

223

answers:

2

Hello SOers,
I'm wondering if it's actually possible to draw a polygon over a complete town with the GoogleMaps API V3 or the V2?

If so could you please give an example on how to do it (choose your favorite town :)).

I've some difficulties understanding this API.

Thanks.

A: 

I don't think it is possible to draw a polygon around a town boundary automatically; you need to know the coordinates of the corners, and then you draw a polygon connecting those points (so the information about where the town boundaries are doesn't come from Google Maps, but from your application).

Then, you can create a polygon like so:

  var polygon = new GPolygon([
    new GLatLng(lat, lon - lonOffset),
    new GLatLng(lat + latOffset, lon),
    new GLatLng(lat, lon + lonOffset),
    new GLatLng(lat - latOffset, lon),
    new GLatLng(lat, lon - lonOffset)
  ], "#f33f00", 5, 1, "#ff0000", 0.2);
  map.addOverlay(polygon);

(Example code taken from the API docs.)

pkaeding
A: 

If you want to outline the boundary of a town, you're going to need some additional data that tells you where to draw. Shape files are good for this sort of thing, and it is generally easy to find free or low-cost files that provide boundaries of cities, states, counties, countries, etc. Just do a google search for "[CityName] shape files" or something like that.

Once you have a good shape file, you can query it for all of the points in the boundary. You would then call the method to draw a polygon with those points in the maps api.

Todd Benning
Is there any functions in the maps api that can make queries on local shape files?
Frank
To be honest, I haven't done much with the api aside from basic drawing, but a quick Google search finds this article that discusses how to convert shape files to a text format that can be handled more easily: http://www.googlemapsbook.com/2006/08/30/shapefiles/
Todd Benning