Ticket #436: GoogleMapsPostcodeFix.txt

File GoogleMapsPostcodeFix.txt, 5.4 KB (added by networkfusion, 3 years ago)

This Patch fixes the postcode search on google maps.

Line 
1Index: wifidog/js/hotspots_status_map.js
2===================================================================
3--- wifidog/js/hotspots_status_map.js   (revision 1408)
4+++ wifidog/js/hotspots_status_map.js   (working copy)
5@@ -41,6 +41,9 @@
6  * @link       http://www.wifidog.org/
7  */
8 
9+// Create a Global array that will contain refs to markers
10+var markers = new Array();
11+
12 // Translations
13 function HotspotsMapTranslations(browser_support, homepage, show_on_map, loading)
14 {
15@@ -63,7 +66,7 @@
16         this.map.addControl(new GMapTypeControl());
17 
18         // Create the array that will contain refs to markers
19-        this.markers = Array();
20+        //markers = Array();
21 
22         // Init source url
23         this.xml_source = null;
24@@ -84,31 +87,40 @@
25     // Save the reference to "this" in a global var for async needs
26     var self = this;
27     GDownloadUrl("geocoder.php?postal_code=" + postal_code, function(data, responseCode) {
28-           var root_node = GXml.parse(data).documentElement
29+        // To ensure against HTTP errors that result in null or bad data,
30+        // always check status code is equal to 200 before processing the data
31+        if(responseCode == 200) {
32+               var root_node = GXml.parse(data).documentElement
33                self.findClosestHotspotByCoords(new GLatLng(GXml.value(root_node.getElementsByTagName("lat")[0]), GXml.value(root_node.getElementsByTagName("long")[0])));
34+        }else if(responseCode == -1) {
35+    alert("Data request timed out. Please try later.");
36+  } else {
37+    alert("Request resulted in error. Check XML file is retrievable.");
38+  }
39+
40        });
41 }
42 
43+
44 HotspotsMap.prototype.findClosestHotspotByPostalCode = function(postal_code)
45 {
46-    if (postal_code != undefined && this.markers.length > 0) {
47+    if (postal_code != undefined && markers.length > 0) {
48         this.getGPointFromPostalCode(postal_code);
49     }
50 }
51 
52 HotspotsMap.prototype.findClosestHotspotByCoords = function(coord)
53 {
54-    if (coord != null && this.markers.length > 0) {
55+    if (coord != null && markers.length > 0) {
56         // Init values
57         var dist = null;
58         var hotspot_id = null;
59 
60         // For each registered markers
61-        for(i in this.markers) {
62-                  if(this.markers[i] && this.markers[i].getPoint) {
63+        for(i in markers) {
64+                  if(markers[i] && markers[i].getPoint) {
65                    // Compute the distance in meters between the two points
66-                   tmp = coord.distanceFrom( this.markers[i].getPoint());
67-
68+                   tmp = coord.distanceFrom( markers[i].getPoint());
69                    if(dist == null || tmp < dist) {
70                        dist = tmp
71                        hotspot_id = i;
72@@ -260,7 +272,7 @@
73 HotspotsMap.prototype.openInfoBubble = function(bubbleId)
74 {
75     // Trigger click ( NB. markers is a global var )
76-    GEvent.trigger(this.markers[bubbleId], "click");
77+    GEvent.trigger(markers[bubbleId], "click");
78 }
79 
80 HotspotsMap.prototype.loadHotspotsStatus = function()
81@@ -334,13 +346,14 @@
82 
83             // Prepare fragment that will go in the sidebar
84             var html = this.buildHtmlFromHotspot(hotspots[i], markerIcon);
85-            html_list += html + "<br /><br /><a href=\"javascript:" + this.external_object_name +".openInfoBubble('" + GXml.value(hotspotId[0]) + "');\">" + this.translations.show_on_map + "</a><hr width='95%'/>";
86+            html_list += html + "<br /><br /><a href=\"javascript:" + this.external_object_name +".openInfoBubble('" + markers.length + "');\">" + this.translations.show_on_map + "</a><hr width='95%'/>";
87 
88             // Create, save as ID and add the marker
89             var marker = this.createInfoBubble(point, markerIcon, html);
90 
91             // markers is a global var
92-            this.markers[GXml.value(hotspotId[0])] = marker;
93+            //markers[GXml.value(hotspotId[0])] = marker;
94+            markers[markers.length] = marker;
95             this.map.addOverlay(marker);
96         }
97     }
98@@ -351,8 +364,8 @@
99 
100 HotspotsMap.prototype.redraw = function()
101 {
102-    for (i = 0;i < this.markers.length;i++) {
103-        this.map.removeOverlay(this.markers[i]);
104+    for (i = 0;i < markers.length;i++) {
105+        this.map.removeOverlay(markers[i]);
106     }
107 
108     this.hotspots_info_list.innerHTML = this.translations.loading;
109@@ -382,4 +395,4 @@
110 HotspotsMap.prototype.setXmlSourceUrl = function(xml_source)
111 {
112     this.xml_source = xml_source;
113-}
114\ No newline at end of file
115+}
116
117Index: wifidog/templates/sites/hotspots_map.tpl
118===================================================================
119--- wifidog/templates/sites/hotspots_map.tpl    (revision 1408)
120+++ wifidog/templates/sites/hotspots_map.tpl    (working copy)
121@@ -61,7 +61,7 @@
122             <div id="map_postalcode_overlay">
123                 {"Enter your postal code"|_}:<br/>
124                 <input type="text" id="postal_code" size="10"><p/>
125-                <input type="button" value="{"Show"|_}" onclick="toggleOverlay('map_postalcode_overlay'); p = document.getElementById('postal_code'); hotspots_map.findClosestHotspotByPostalCode(p.value);">
126+                <input type="button" value="{"Show"|_}" onclick="toggleOverlay('map_postalcode_overlay'); p = document.getElementById('postal_code'); HotspotsMap.prototype.findClosestHotspotByPostalCode(p.value);">
127             </div>
128 
129             <input type="button" value="{"Refresh map"|_}" onclick="hotspots_map.redraw();">
130@@ -88,4 +88,4 @@
131 {*
132     END section MAINCONTENT
133 *}
134-{/if}
135\ No newline at end of file
136+{/if}