views:

741

answers:

1

The following is an example of how to greate a polygon in the google maps API.
What is the purpose of latOffset and lonOffset?
We're creating an array of points to make a polygon, but what exactly is the offset doing?

var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
map.addControl(new GSmallMapControl());
GEvent.addListener(map, 'click', function(overlay, latlng) {
  var lat = latlng.lat();
  var lon = latlng.lng();
  var latOffset = 0.01;
  var lonOffset = 0.01;
  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);
});
+4  A: 

It's just building a diamond around the point (lat,lon).

     *

  *  O  *

     *

Lat is latitude and lon is longitude. The offsets are how far away from the center point the corners of the diamond are.

I wonder how well it works at the north pole.

Nosredna
It is amusing how often this type of pole-ignoring stuff seems to come up in 'practical' GIS code. Conveniently, though, the north pole doesn't really exist on google maps, since it uses the Mercator projection the poles are infinitely far away in pixel space.
Doug McClean
I so rarely travel to the north pole anyway.
Nosredna