/* * mashup.js * * Computer Science 50 * Problem Set 8 * * Implements mashup. */ // mashup's geocoder var geocoder = null; // map's home var home = new GLatLng(42.375892, -71.114792); // mashup's Google Map var map = null; /* * void * addMarker(point, xhtml) * * Adds marker with given xhtml at given point on map. */ function addMarker(point, xhtml) { // instantiate marker var marker = new GMarker(point); // prepare info window for city GEvent.addListener(marker, "click", function() { map.openInfoWindowHtml(point, xhtml); }); // add marker to map map.addOverlay(marker); } /* * void * go() * * Pans map to desired location if possible, else yells at user. */ function go() { // TODO: go to location // update markers update(); } /* * void * load() * * Loads (and configures) Google Map. */ function load() { // ensure browser supports Google Maps if (!GBrowserIsCompatible()) return; // instantiate geocoder geocoder = new GClientGeocoder(); // resize map's container resize(); // instantiate map map = new GMap2(document.getElementById("map")); // center map on home map.setCenter(home, 13); // TODO: add control(s) // update markers anytime user drags or zooms map GEvent.addListener(map, "dragend", update); GEvent.addListener(map, "zoomend", update); // resize map anytime user resizes window GEvent.addDomListener(window, "resize", resize); // update markers update(); // give focus to text field document.getElementById("q").focus(); } /* * void * resize() * * Resizes map's container to fill area below form. */ function resize() { // prepare to determine map's height var height = 0; // check for non-IE browsers if (window.innerHeight !== undefined) height += window.innerHeight; // check for IE else if (document.body.clientHeight !== undefined) height += document.body.clientHeight; // leave room for logo and form height -= 140; // maximize map's height if room if (height > 0) { // adjust height via CSS document.getElementById("map").style.height = height + "px"; // ensure map exists if (map) { // resize map map.checkResize(); // update markers update(); } } } /* * void * unload() * * Unloads Google Map. */ function unload() { // unload Google's API GUnload(); } /* * void * update() * * Updates map with markers for largest <= 5 cities within view. * Also displays marker for home if within view. */ function update() { // clear any existing markers map.clearOverlays(); // get map's bounds var bounds = map.getBounds(); // mark home if within bounds if (bounds.containsLatLng(home)) { // prepare XHTML var xhtml = "Home Sweet Home"; // add marker to map addMarker(home, xhtml); } // TODO: contact proxy, add markers }