1 /* 2 * SIMPLICITE - runtime & framework 3 * http://www.simplicite.fr 4 * Copyright (c)2006-2013 Simplicite Software. All rights reserved. 5 */ 6 7 /** 8 * Google Maps tools 9 * @class 10 */ 11 Simplicite.Gmap = { 12 13 lat: 48.8566667, 14 lng: 2.3509871, 15 zoom: 13, 16 17 map: undefined, 18 bounds: undefined, 19 infoWindow: undefined, 20 21 /** @ignore */ 22 _getInt: function(val, def) { 23 var v = parseInt(val == undefined ? "0" : val); 24 if (isNaN(v) || v == 0) v = def; 25 return v; 26 }, 27 28 /** @ignore */ 29 _getFloat: function(val, def) { 30 if (val && typeof(val)=="string") val = val.replace(",","."); 31 var v = parseFloat(val == undefined ? "0" : val); 32 if (isNaN(v) || v == 0) v = def; 33 return v; 34 }, 35 36 initMap: function(id) { 37 var d = $(id); 38 if (d != undefined) { 39 try { 40 this.map = new google.maps.Map($(id), { 41 center: new google.maps.LatLng(this.lat, this.lng), 42 zoom: this.zoom, 43 mapTypeId: google.maps.MapTypeId.ROADMAP 44 }); 45 this.bounds = new google.maps.LatLngBounds(); 46 this.map_markers = new Array(); 47 return true; 48 } catch(e) { 49 Simplicite.Tools.error("Google map init error", e); 50 return false; 51 } 52 } 53 }, 54 55 map_markers: undefined, 56 57 addMarker: function(lat, lng, zoom, info, center) { 58 if (this.map == undefined) return false; 59 60 var la = this._getFloat(lat, this.lat); 61 var ln = this._getFloat(lng, this.lng); 62 var z = this._getInt(zoom, this.zoom); 63 64 var p = new google.maps.LatLng(la, ln); 65 this.bounds.extend(p); 66 67 if (z != this.zoom) { 68 this.map.setZoom(z); 69 this.zoom = z; 70 } 71 72 var m = new google.maps.Marker({ position: p, map: this.map }); 73 this.map_markers.push(m); 74 75 if (info != undefined && info != "") { 76 var self = this; 77 if (self.infoWindow == undefined) 78 self.infoWindow = new google.maps.InfoWindow({ content: "" }); 79 google.maps.event.addListener(m, "click", function() { 80 self.infoWindow.setContent(info); 81 self.infoWindow.open(this.map, m); 82 }); 83 } 84 if (center) 85 this.map.setCenter(p); 86 else 87 this.map.fitBounds(this.bounds) 88 89 return true; 90 }, 91 removeMarkers: function() { 92 if (this.map != undefined) 93 while (this.map_markers[0]) 94 this.map_markers.pop().setMap(null); 95 }, 96 /** @deprecated */ 97 point: function(lat, lng, zoom, icon, info) { 98 this.addMarker(lat, lng, zoom, info, false); 99 }, 100 101 map_polygons: undefined, 102 103 addRectangle: function(lat1, lng1, lat2, lng2, color) { 104 if (this.map == undefined) return false; 105 106 var la1 = this._getFloat(lat1, this.lat); 107 var ln1 = this._getFloat(lng1, this.lng); 108 var la2 = this._getFloat(lat2, this.lat); 109 var ln2 = this._getFloat(lng2, this.lng); 110 111 if (color == undefined) color = "#FF0000"; 112 113 var polygon = new google.maps.Polygon({ map: this.map, strokeColor: color, strokeWeight: 5, strokeOpacity: 1, fillColor: color, fillOpacity: 0.2 }); 114 this.map_rectangles.push(polygon); 115 polygon.setPath([ 116 new google.maps.LatLng(la1, ln1), 117 new google.maps.LatLng(la1, ln2), 118 new google.maps.LatLng(la2, ln2), 119 new google.maps.LatLng(la2, ln1), 120 new google.maps.LatLng(la1, ln1) 121 ]); 122 123 return true; 124 }, 125 removeRectangles: function() { 126 if (this.map != undefined) 127 while (this.map_rectangles[0]) 128 this.map_rectangles.pop().setMap(null); 129 }, 130 /** @deprecated */ 131 rectangle: function(lat1, lng1, lat2, lng2, color) { 132 this.addRectangle(lat1, lng1, lat2, lng2, color); 133 }, 134 135 clearMap: function(id) { 136 this.removeMarkers(); 137 this.removeRectangles(); 138 }, 139 140 simpleDisplay: function(id, lat, lng, zoom, marker, info) { 141 if (this.map == undefined) { 142 this.initMap(id); 143 } else { 144 this.clearMap(); 145 } 146 if (this.map == undefined) return; 147 this.addMarker(lat, lng, zoom, info, this.map_markers.length == 0); 148 }, 149 150 geocoder: undefined, 151 initGeocoder: function() { 152 try { 153 this.geocoder = new google.maps.Geocoder(); 154 } catch(e) { 155 Simplicite.Tools.error("Google map geocoder init error", e); 156 return false; 157 } 158 }, 159 160 simpleLocalize: function(address, callback, notify) { 161 if (this.geocoder == undefined) { 162 this.initGeocoder(); 163 } 164 if (this.geocoder == undefined) return; 165 166 if (!notify) notify = function(msg) { alert(msg); }; 167 168 var self = this; 169 self.geocoder.geocode({ 170 address: address 171 }, function(response, status) { 172 if (status == "ZERO_RESULTS") { 173 notify("No answers for [" + address + "]"); 174 } else if (status != "OK") { 175 notify("Status Code: " + status); 176 } else { 177 if (response.length > 1) { 178 notify(response.length + " answers for [" + address + "], only first one displayed"); 179 } 180 181 var r = response[0]; 182 if (callback != undefined) callback.call(self, r.geometry.location, r.formatted_address); 183 } 184 }); 185 }, 186 187 simpleLocalizeAndDisplay: function(id, address, zoom, marker, info, callback, notify) { 188 var self = this; 189 if (address != undefined && address != "") { 190 self.simpleLocalize(address, function(location, address) { 191 if (info == undefined) info = address; 192 193 self.simpleDisplay(id, location.lat(), location.lng(), zoom, marker, info); 194 195 if (callback != undefined) callback.call(self, location, address); 196 }, notify); 197 } else 198 self.simpleDisplay(id, this.lat, this.lng, zoom, marker, info); 199 }, 200 201 dms2lat: function(s) { return (s.substring(6,7) == "N" ? 1 : -1) * (this.dms(s.substring(0,2) + "," + s.substring(2,4) + "," + s.substring(4,6))); }, 202 dms2lng: function(s) { return (s.substring(7,8) == "E" ? 1 : -1) * (this.dms(s.substring(0,3) + "," + s.substring(3,5) + "," + s.substring(5,7))); }, 203 dms: function(s) { var d = s.split(","); return parseInt(d[0]) + parseInt(d[1])/60 + parseInt(d[2])/3600; } 204 205 }; 206