Lat49 ads are selected based on latitude, longitude and zoom level. To display dynamically changing ads within a map-based application, a listener function has to be provided that calls the ad updating method when some map event occurs, and the updating method should be passed some event-related coordinates. What events and coordinates are used is up to the application writer to choose.
The zoom level for the ad can be easily got by converting the level of the displayed map (using one of the methods described in Section 4.3). The geographic coordinates will depend on the kind of event that triggers the updating of the ad. For example, if panning events are used, then the ad can be updated based on the coordinates of the center of the map (as illustrated in Example 5-3). Or, the position of a map marker can be used when it is added or selected. Even mouse clicks within the map pane can be used if it makes sense within the context of the application.
When working with Google Maps, listeners for specific events are added using the GEvent.addListener(object, event, handler) method. The handler method should call the ad updating method updateAdByLatLon and pass in the new coordinates, as in the example below for triggering on mouse clicks:
Example 5-1. Adding a mouse click listener when using Google Maps
GEvent.addListener(map, "click", function(overlay, latlng) {
var lat = latlng.lat();
var lng = latlon.lng();
var zoom = Lat49.Tile.convertGMap2Zoom(map.getZoom());
Lat49.updateAdByLatLon("adcontainer", lat, lng, zoom);
}); |
When using the Virtual Earth API, listeners are added using the VEMap.AttachEvent method. Things are a bit more complicated over the Google Maps example above because the latitude and longitude cannot be got directly from the VE MapEvent object. Instead, the screen click position has to be converted via the VEMap object ve_map (a global variable in the example below).
Example 5-2. Adding a mouse click listener when using Virtual Earth
ve_map.AttachEvent("onclick", function(e) {
var zoom = Lat49.Tile.convertLiveZoom(e.zoomLevel);
var latlon = ve_map.PixelToLatLong(new VEPixel(e.mapX, e.mapY));
Lat49.updateAdByLatLon("adcontainer", latlon.Latitude, latlon.Longitude, zoom);
}); |