/**************************************** * Gmap location input related functions ****************************************/ var geocoder = null; jQuery(document).ready(function() { var count = jQuery("#gmap-count").val(); for(var i=0; i< count;i++){ gmaplocationfield_init('gmap-'+i); } }); /** * Initializes the JS code calls */ function gmaplocationfield_init(id){ jQuery("[name=country_"+id+"]").change(gmaplocationfield_load_states); jQuery("[name=state_"+id+"]").change(gmaplocationfield_load_cities); jQuery("[name=city_"+id+"]").change(gmaplocationfield_load_address); if(GMAP_LOADED){ jQuery("#div_"+id).width(jQuery("#container_"+id).width()); jQuery("#div_"+id).height(300); jQuery("#div_"+id).gmap({controls: ['GLargeMapControl'], markers: []}); geocoder = new GClientGeocoder(); } } /** * Takes the selected value from [name=country] and loads states info inside * the [name=state] field using an AJAX call. * * @param aCountry Event object whent it is called from a JQuery Event | String with the country code * @param aId Undefined when it is called from a JQuery Event | DOM id from the gmap context * @param aGeocode Undefined when it is called from a JQuery Event | Boolean */ function gmaplocationfield_load_states(aCountry,aId,aGeoCode){ var id,country,geocode; if(aCountry.type){ id = jQuery(this).attr("name").split("_")[1]; country = jQuery(this).val(); geocode = true; } else{ id = aId; country = aCountry; geocode = aGeoCode; } if(geocode){ var country_ = jQuery("[name=country_"+id+"] :selected").text(); gmaplocationfield_geocode(id,country_,5,true); } jQuery("[name=state_"+id+"]").empty(); jQuery("[name=state_"+id+"]").append(""); jQuery("[name=city_"+id+"]").empty(); jQuery("[name=city_"+id+"]").append(""); if(country!="--"){ $.get(ELGG_WWWROOT+"pg/gmap/states/"+country,function(data){ if(data){ eval(data); for(var i=0; i< states.length;i++){ var state = states[i]; jQuery("[name=state_"+id+"]").append(""); } } }); } } /** * Takes the selected value from [name=state] and loads the cities info inside * the [name=city] field using an AJAX call * * @param aState Event object when it is called from a JQuery event | String witht the state code * @param aId Undefined when it is called from a JQuery Event | DOM id from the gmap context * @param aGeocode Undefined when it is called from a JQuery Event | Boolean */ function gmaplocationfield_load_cities(aState,aId,aGeoCode){ var id,state,geocode; if(aState.type){ id = jQuery(this).attr("name").split("_")[1]; state = jQuery(this).val(); geocode=true; } else{ id = aId; state = aState; geocode = aGeoCode; } if(geocode){ var address = jQuery("[name=state_"+id+"] :selected").text(); address += ',' + jQuery("[name=country_"+id+"] :selected").text(); gmaplocationfield_geocode(id,address,7,true); } jQuery("[name=city_"+id+"]").empty(); jQuery("[name=city_"+id+"]").append(""); if(state!="--"){ $.get(ELGG_WWWROOT+"pg/gmap/cities/"+state,function(data){ if(data){ eval(data); for(var i=0; i< cities.length;i++){ var city = cities[i]; jQuery("[name=city_"+id+"]").append(""); } } }); } } /** * Zoom in the map to city, state, country */ function gmaplocationfield_load_address(aCity){ var id; if (aCity.target) { id = jQuery(this).attr("name").split("_")[1]; } else{ id = aCity; } var address = jQuery("[name=city_"+id+"] :selected").text(); var address = jQuery("[name=city_"+id+"] :selected").text(); address += ',' + jQuery("[name=state_"+id+"] :selected").text(); address += ',' + jQuery("[name=country_"+id+"] :selected").text(); gmaplocationfield_geocode(id,address,15,true); } /** * Look up for an address and zoom in to it with the specified zoom level. * * @param String id Field identifier * @param String address Address for look up * @param intenger zoom Zoom level * @param boolean showMarker Put a marker in the result address */ function gmaplocationfield_geocode(id,address, zoom, showMarker) { var map_id = "div_"+id; var draggable_ = true; if (geocoder) { geocoder.getLatLng(address,function(point) { if (point) { $.gmap.maps[map_id].setZoom(zoom); $.gmap.maps[map_id].panTo(point); if(showMarker){ if(!$.gmap.markers[map_id]){ $.gmap.markers[map_id] = new GMarker(point,{draggable:draggable_}); $.gmap.maps[map_id].addOverlay($.gmap.markers[map_id]); GEvent.addListener($.gmap.markers[map_id], "dragend", function(point) { jQuery("#"+id).attr('value',point.lat()+","+point.lng()); }); } $.gmap.markers[map_id].setLatLng(point); jQuery("#"+id).attr('value',point.lat()+","+point.lng()); if($.gmap.markers[map_id].isHidden()){ $.gmap.markers[map_id].show(); } } } } ); } } function gmaplocationfield_update_marker(id,newpoint,zoom){ var map_id = "div_"+id; var draggable_ = true; $.gmap.maps[map_id].panTo(newpoint); $.gmap.maps[map_id].setZoom(zoom); $.gmap.maps[map_id].setCenter(newpoint); if(!$.gmap.markers[map_id]){ $.gmap.markers[map_id] = new GMarker(newpoint,{draggable:draggable_}); $.gmap.maps[map_id].addOverlay($.gmap.markers[map_id]); GEvent.addListener($.gmap.markers[map_id], "dragend", function(point) { jQuery("#"+id).attr('value',point.lat()+","+point.lng()); }); } $.gmap.markers[map_id].setLatLng(newpoint); jQuery("#"+id).attr('value',newpoint.lat()+","+newpoint.lng()); if($.gmap.markers[map_id].isHidden()){ $.gmap.markers[map_id].show(); } } if(GMAP_LOADED){ $.gmap = { maps: {}, markers: {}, marker: function(m) { if (!m) { return null; } else if (m.lat == null && m.lng == null) { return $.gmap.marker($.gmap.readLatLng(m)); } else if (m.lat == '' || m.lng == '') { return null; } else { var marker = new GMarker(new GLatLng(m.lat, m.lng)); if (m.txt) { GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(m.txt); currentMarker = marker; }); } return marker; } }, readLatLng: function(elem) { var latElem = $(".latitude", elem)[0]; var lngElem = $(".longitude", elem)[0]; if (latElem && lngElem) { return { lat:parseFloat($(latElem).attr("title")), lng:parseFloat($(lngElem).attr("title")), txt:$(elem).attr("title") } } else { return null; } }, mapCount: 1 }; $.fn.gmap = function(options) { // If we aren't supported, we're done if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this; // Sanitize options if (!options || typeof options != 'object')options = {}; options.mapOptions = options.mapOptions || {}; options.markers = options.markers || []; options.controls = options.controls || {}; // Map all our elements return this.each(function() { // Make sure we have a valid id if (!this.id) this.id = "gmap" + $.gmap.mapCount++; // Create a map and a shortcut to it at the same time map = $.gmap.maps[this.id] = new GMap2(this, options.mapOptions); geocoder = new GClientGeocoder(); map.setCenter(new GLatLng(0,0),0); // Add controls to our map for (var i = 0; i < options.controls.length; i++) { var c = options.controls[i]; eval("map.addControl(new " + c + "());"); } // Poner todos los marcadores asociados en el mapa var marker = null; var i = 0; for (; i < options.markers.length; i++) { if (marker = $.gmap.marker(options.markers[i])){ map.addOverlay(marker); bounds.extend(marker.getPoint()); } } // Si tenemos al menos un marcador --> actualizar los bordes y zoom if ( i != 0){ map.setZoom(map.getBoundsZoomLevel(bounds) ); map.setCenter(bounds.getCenter()); } map.addControl(new GMapTypeControl()); // map.setMapType(G_HYBRID_MAP); }); }; /*************************/ var latlngs = new Array(); var bounds = new GLatLngBounds(); String.prototype.trim = function () { return this.replace(/^\s*/, "").replace(/\s*$/, ""); } function processLocations(elementId, delete_button_txt, editable) { ms = new Array(); for (var i = 0; i < latlngs.length; i++) { var splitted_array = latlngs[i].split("|"); m = new Object(); m.lat = splitted_array[0]; m.lng = splitted_array[1]; if (editable) { var txt = "" + "" + "
" + unescape(splitted_array[2]) + "
"; m.txt = txt; } else { var txt = "" + "
" + splitted_array[2] + "
"; m.txt = txt; } m.add = unescape(splitted_array[2]); ms.push(m); } return ms; } /** * Utility function to calculate the appropriate zoom level for a * given bounding box and map image size. Uses the formula described * in the Google Mapki (http://mapki.com/). * @return zoom level. */ function best_zoom(width, height, max_lat, min_lat, max_lng, min_lng) { var dlat = Math.abs((max_lat) - (min_lat)); var dlon = Math.abs((max_lng) - (min_lng)); if(dlat == 0 && dlon == 0) return 15; // Center latitude in radians var clat = Math.PI*(min_lat + max_lat)/360.; var C = 0.0000107288; var z0 = Math.ceil(Math.log(dlat/(C*height))/Math.LN2); var z1 = Math.ceil(Math.log(dlon/(C*width*Math.cos(clat)))/Math.LN2); return (z1 > z0) ? 17 - z1 : 17 - z0; } /*********** Utility functions for general map ************/ /** * A general helper function for creating html elements.
as default element type * @author Esa 2008 * used for infowindows and sidebar */ function createElem(opt_className, opt_html, opt_tagName) { var tag = opt_tagName||"div"; var elem = document.createElement(tag); if (opt_html) elem.innerHTML = opt_html; if (opt_className) elem.className = opt_className; return elem; } /** * sidebar with categories * @author Esa 2008 */ function SideBar(block_element, opt_options){ var opts = opt_options||{}; this.division = createElem("sidebar-contents"); block_element.appendChild(this.division); this.show = function(){this.division.style.display = "block"}; this.hide = function(){this.division.style.display = "none"}; this.cats = []; this.addEntry = function(point,opt_options){ var opts = opt_options||{}; var iLabel = opts.iLabel||2; var label = createElem("sidebar-entry", point.textArray[iLabel], "a"); label.href = "#"; label.style.display = "block"; label.onclick = function(){GEvent.trigger(point.marker,'click'); return false};//x-browser label.onfocus = function(){GEvent.trigger(point.marker,'click'); return false}; this.division.appendChild(label); GEvent.addListener(point.marker,'click',function(){label.focus(); return false}); } this.clear = function(){ while (this.division.firstChild) { this.division.removeChild(this.division.firstChild); } } } /** * category to a sidebar * @author Esa 2008 */ function BarCategory(sideBar, catName, opt_options){ var me = this; var opts = opt_options||{}; me.division = createElem("sidebar-cat"); var cssClasses = "sidebar-cat-header cat-header-"+catName; var cat = createElem(cssClasses); me.pin = createElem("sidebar-cat-image",null,"img"); me.pin.src = opts.icon.image; cat.appendChild(me.pin); var check = createElem("sidebar-cat-check",null,"input"); me.header = cat; check.type = "checkbox"; check.checked = opts.checked||false; cat.appendChild(check); var checkLabel = createElem("sidebar-cat-label",catName,"span"); cat.appendChild(checkLabel); me.show = function(){ me.division.style.display = "block"; me.pin.style.visibility = "visible"; } me.hide = function(){ me.division.style.display = "none"; me.pin.style.visibility = "hidden"; } me.hilight = function(){me.header.className = cssClasses +" hilight-cat-header"}; me.lolight = function(){me.header.className = cssClasses}; sideBar.division.appendChild(cat); sideBar.division.appendChild(me.division); sideBar.cats[catName] = me; me.markers = []; me.showMarkers = function(){ for(var i=0;i