File: geocode.js

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (264 lines) | stat: -rw-r--r-- 9,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// This file is part of Zoph.
//
// Zoph is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Zoph is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Zoph; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

var zGeocode=function() {
    var geourl="https://secure.geonames.org/search?style=SHORT&username=zoph&q=";
    var wikiurl="https://secure.geonames.org/wikipediaSearch?username=zoph&q=";
    var url;
    var geotag="geoname";
    var wikitag="entry"
    var xmltag;

    function checkGeocode() {
        // To prevent overwrite of tediously set lat & lon
        // you need to click the 'find' button twice, if a lat&lon have
        // already been set.
        var lat=document.getElementById("lat").value;
        var lon=document.getElementById("lon").value;

        if (lat==0 && lon==0) {
            enableGeocode();
        } else {
            disableGeocode();
        }
    }

    function enableGeocode() {
        var button=document.getElementById("geocode");
        button.className=button.className.replace("geo_disabled", "geocode");
        button.onclick=zGeocode.startGeocode;
    }

    function disableGeocode() {
        var button=document.getElementById("geocode");
        button.className=button.className.replace("geocode", "geo_disabled");
        button.onclick=zGeocode.enableGeocode;
    }

    function startGeocode() {
        url=geourl;
        xmltag=geotag;
        var objQuery={
            title:      document.getElementById("title").value,
            address:    document.getElementById("address").value,
            address2:   document.getElementById("address2").value,
            city:       document.getElementById("city").value,
            state:      document.getElementById("state").value,
            zip:        document.getElementById("zip").value,
            country:    document.getElementById("country").value
        };

        // remove empty items
        for (var i in objQuery) {
            if (trim(objQuery[i])==="") {
                delete objQuery[i];
            }
        }

        geocode(objQuery);
    }

    function geocode(objQuery) {
        var divResult=document.getElementById("geocoderesults");
        var query="";
        for (var i in objQuery) {
            if (trim(query)!=="") {
                query += ", ";
            }
            query+=objQuery[i];
        }

        divResult.innerHTML="searching for...<br>" + query;

        var http=new XMLHttpRequest();
        http.open("GET", url + encodeURI(query), true);

       http.onreadystatechange=function() {
            zGeocode.handleGeocode(http, objQuery);
        };
        http.send(null);
    }

    function handleGeocode(http, objQuery) {
        var divResult=document.getElementById("geocoderesults");
        var b;

        if (http.readyState == 4) {
            if (http.status == 200) {
                var response=http.responseXML;
                var geonames=response.getElementsByTagName(xmltag);
                if (geonames.length > 0) {
                    displayGeocode(geonames, divResult, 0);
                } else {
                    // No results, let's try again with some less fields
                    if (objQuery.zip) {
                        delete (objQuery.zip);
                    } else if (objQuery.address2) {
                        delete objQuery.address2;
                    } else if (objQuery.title) {
                        delete objQuery.title;
                    } else if (objQuery.address) {
                        delete objQuery.address;
                    } else if (objQuery.state) {
                        delete objQuery.state;
                    } else if (objQuery.country) {
                        delete objQuery.country;
                    }

                    if ((Object.keys(objQuery).length == 0) && (url != wikiurl)) {

                        objQuery={
                            title:      document.getElementById("title").value
                        }
                        url=wikiurl;
                        xmltag=wikitag;
                    }
                    if (Object.keys(objQuery).length > 0) {
                        geocode(objQuery);
                    } else {
                        divResult.innerHTML="";
                        b=document.createElement("b");
                        b.innerHTML=translate['Nothing found'];
                        divResult.appendChild(b);
                        return;
                    }

                }

            } else if (http.status == 0) {
                divResult.innerHTML="";
                b=document.createElement("b");
                b.innerHTML=translate['An error occurred'];
                divResult.appendChild(b);
            }
        }
    }

    function displayGeocode(geonames, divResult, result) {
        var total=geonames.length;
        var titlefield=document.getElementById("title");
        var title, lat, lon;

        // Define zoomlevels for different kinds of respones
        // see http://www.geonames.org/export/codes.html
        var zoomlevels= {
            "A": 6, // Country, state, region
            "H": 8, // Stream, lake
            "L": 15, // Parks, area
            "P": 12, // City, village
            "R": 17, // Road, railroad
            "S": 18, // Spot, building, farm
            "T": 12, // Mountain, hill, rock
            "U": 5,   // Undersea
            "V": 14  // Forest, heath
        };

        //define zoomlevels for different "features" in Wikipedia
        // see http://www.geonames.org/wikipedia/wikipedia_features.html

        var features={
            "city":             12,
            "railwaystation":   18,
            "edu":              17,
            "waterbody":        8,
            "landmark":         18,
            "adm2nd":           13,
            "mountain":         12,
            "adm3rd":           10,
            "airport":          16,
            "river":            8,
            "isle":             14,
            "event":            17,
            "adm1st":           15,
            "glacier":          16,
            "country":          6,
            "forest":           14,
            "pass":             17,
            "church":           18
        };

        var zoomlevel=12;

        for (var tag of geonames[result].childNodes) {
            var content=tag.textContent;
            switch(tag.nodeName) {
                case "title":
                    title=content;
                    break;
                case "toponymName":
                    title=content;
                    break;
                case "lat":
                    lat=content;
                    break;
                case "lng":
                case "lon":
                    lon=content;
                    break;
                case "fcl":
                    zoomlevel=zoomlevels[content];
                    break;
                case "feature":
                    zoomlevel=features[content];
                    break;
            }
        }
        if (lat && lon) {
            document.getElementById("lat").value=lat;
            document.getElementById("lon").value=lon;
            document.getElementById("mapzoom").value=zoomlevel;
            zMaps.updateMap();
        }
        var left=document.createElement("input");
        left.setAttribute("type", "button");
        left.className="leftright";
        left.setAttribute("value","<");

        var right=document.createElement("input");
        right.setAttribute("type", "button");
        right.setAttribute("value",">");
        right.className="leftright";

        if (result===0) {
            left.disabled=true;
        } else if ((result + 1) == total) {
            right.disabled=true;
        }

        right.onclick=function() { displayGeocode(geonames, divResult, result + 1); };
        left.onclick=function() { displayGeocode(geonames, divResult, result - 1); };
        // This is a little bit of a hidden feature, click the title of the
        // found place to set this place's title.
        var b=document.createElement("b");
        b.onclick=function() { titlefield.value=title; };
        b.innerHTML=title;
        var text=document.createTextNode((result + 1) + " / " + total);

        divResult.innerHTML="";
        divResult.appendChild(b);
        divResult.appendChild(document.createElement("br"));
        divResult.appendChild(text);
        divResult.appendChild(document.createElement("br"));
        divResult.appendChild(left);
        divResult.appendChild(right);
        disableGeocode();
    }
    return {
        checkGeocode:checkGeocode,
        enableGeocode:enableGeocode,
        startGeocode:startGeocode,
        handleGeocode:handleGeocode
    };
}();