246 Part III — Google Map Hacks
-97.58638143539429, 35.278137436300966,
‘Sales Office’));
mapobjects.push(new MapObject(-97.59279727935791, 35.283585084492245,
-97.58638143539429, 35.282376473923584,
‘Packaging/Delivery Office’));
mapobjects.push(new MapObject(-97.59277582168579, 35.282008632343754,
-97.59017944335938, 35.278137436300966,
‘Admin Office’));
mapobjects.push(new MapObject(-97.59277582168579, 35.27776957546562,
-97.5864028930664, 35.27657839558136,
‘IT/Support Services’));
Now the map just has to trigger a check of each of these objects when the map is clicked.
Identifying the Click Location
By modifying the click event that was used in the previous example, a click on the map can
trigger checking the list of registered areas. This is achieved by iterating over the array of
objects and then calling the inbounds()method on each object. The following fragment
should be placed into the onLoad()function to be initialized with the rest of the application:
GEvent.addListener(map, ‘click’, function(overlay,point) {
message.innerHTML = ‘Empty’;
for(var i=0;i<mapobjects.length;i++) {
mapobjects[i].inbounds(point.x,point.y);
}
});
If it matches, the information will be placed into the information area of the application.
Resetting the Map Location
To prevent the user from getting distracted by other nearby entities on the map, a timeout can
be added to the window that automatically resets the map’s center point location after a given
interval. The method is actually a function of the JavaScript, not Google Maps, but it can be
used to trigger a Google Maps event, for example a reload of map data. The interval is speci-
fied in milliseconds, and a value of 60000 milliseconds (or 60 seconds) is used in the following
example.
This is actually a two-stage process. First, a function that recenters the map and resets the
timeout (so that it is continually active) must be defined:
function recenter() {
map.centerAndZoom(new GPoint(-97.58790493011475, 35.28039711620333), 1);
window.setTimeout(‘recenter()’,60000);
}