]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Mapstraction/js/mxn.google.geocoder.js
5119c00a17e1c6351800eee0e5fb11a19f6a9029
[quix0rs-gnu-social.git] / plugins / Mapstraction / js / mxn.google.geocoder.js
1 /*
2    Copyright (c) 2007, Andrew Turner
3    All rights reserved.
4
5    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9  * Neither the name of the Mapstraction nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
11  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12  */
13
14
15 // Use http://jsdoc.sourceforge.net/ to generate documentation
16
17 // TODO: add reverse geocoding support
18
19 /**
20  * MapstractionGeocoder instantiates a geocoder with some API choice
21  * @param {Function} callback The function to call when a geocode request returns (function(waypoint))
22  * @param {String} api The API to use, currently only 'mapquest' is supported
23  * @param {Function} error_callback The optional function to call when a geocode request fails
24  * @constructor
25  */
26 function MapstractionGeocoder(callback, api, error_callback) {
27   this.api = api;
28         this.callback = callback;
29         this.geocoders = new Object();
30         if(error_callback == null) {
31                 this.error_callback = this.geocode_error
32         } else {
33                 this.error_callback = error_callback;
34         }
35
36   // This is so that it is easy to tell which revision of this file 
37   // has been copied into other projects.
38   this.svn_revision_string = '$Revision: 107 $';
39
40   this.addAPI(api);
41
42 }
43
44
45 /**
46  * Internal function to actually set the router specific parameters
47  */
48 MapstractionGeocoder.prototype.addAPI = function(api) { 
49
50   me = this;
51   switch (api) {
52                 case 'google':
53                         this.geocoders[api] = new GClientGeocoder();
54                         break;
55     case 'mapquest':
56                         //set up the connection to the geocode server
57                         var proxyServerName = "";
58                         var proxyServerPort = "";
59                         var ProxyServerPath = "mapquest_proxy/JSReqHandler.php";
60
61                         var serverName = "geocode.access.mapquest.com";
62                         var serverPort = "80";
63                         var serverPath = "mq";
64                         this.geocoders[api] = new MQExec(serverName, serverPath, serverPort, proxyServerName,
65                                 ProxyServerPath, proxyServerPort );
66                                 
67       break;
68     default:
69       alert(api + ' not supported by mapstraction-geocoder');
70   }
71 }
72 /**
73  * Change the Routing API to use
74  * @param {String} api The API to swap to
75  */
76 MapstractionGeocoder.prototype.swap = function(api) {
77   if (this.api == api) { return; }
78
79   this.api = api;
80   if (this.geocoders[this.api] == undefined) {
81     this.addAPI($(element),api);
82   }
83 }
84
85 /**
86  * Default Geocode error function
87  */
88 MapstractionGeocoder.prototype.geocode_error = function(response) { 
89         alert("Sorry, we were unable to geocode that address");
90 }
91
92 /**
93  * Default handler for geocode request completion
94  */
95 MapstractionGeocoder.prototype.geocode_callback = function(response, mapstraction_geocoder) { 
96   var return_location = new Object();
97         
98         // TODO: what if the api is switched during a geocode request?
99         // TODO: provide an option error callback
100         switch (mapstraction_geocoder.api) {
101                 case 'google':
102                 if (!response || response.Status.code != 200) {
103                 mapstraction_geocoder.error_callback(response);
104                         } else {
105                                 return_location.street = "";
106                                 return_location.locality = "";
107                                 return_location.region = "";
108                                 return_location.country = "";
109
110                                 var place = response.Placemark[0];
111                                 if(place.AddressDetails.Country.AdministrativeArea != null) {
112                                         return_location.region = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
113                                         
114                                         if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea != null) {
115                                                 if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality != null) {
116                                                         return_location.locality = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
117
118                                                         if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare != null)
119                                                                 return_location.street = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
120                                                 }
121                                                 
122                                         }
123                                         
124                                 }
125                                 return_location.country = place.AddressDetails.Country.CountryNameCode;
126                          return_location.address = place.address;       
127
128                 return_location.point = new mxn.LatLonPoint(place.Point.coordinates[1],
129                     place.Point.coordinates[0]);
130                     mapstraction_geocoder.callback(return_location);
131                 }
132                         break;
133                 case 'mapquest':
134                         break;
135         }
136 }
137
138
139 /**
140  * Performs a geocoding and then calls the specified callback function with the location
141  * @param {Object} address The address object to geocode
142  */
143  MapstractionGeocoder.prototype.geocode = function(address) { 
144      var return_location = new Object();
145
146      // temporary variable for later using in function closure
147      var mapstraction_geocoder = this;
148
149      switch (this.api) {
150          case 'google':
151          if (address.address == null || address.address == "")
152          address.address = address.street + ", " + address.locality + ", " + address.region + ", " + address.country
153          this.geocoders[this.api].getLocations(address.address, function(response) { mapstraction_geocoder.geocode_callback(response, mapstraction_geocoder); });
154          break;
155          case 'mapquest':
156          var mqaddress = new MQAddress();
157          var gaCollection = new MQLocationCollection("MQGeoAddress");
158          //populate the address object with the information from the form
159          mqaddress.setStreet(address.street);
160          mqaddress.setCity(address.locality);
161          mqaddress.setState(address.region);
162          mqaddress.setPostalCode(address.postalcode);
163          mqaddress.setCountry(address.country);
164
165          this.geocoders[this.api].geocode(mqaddress, gaCollection);
166          var geoAddr = gaCollection.get(0);
167          var mqpoint = geoAddr.getMQLatLng();
168          return_location.street = geoAddr.getStreet();
169          return_location.locality = geoAddr.getCity();
170          return_location.region = geoAddr.getState();
171          return_location.country = geoAddr.getCountry();
172          return_location.point = new mxn.LatLonPoint(mqpoint.getLatitude(), mqpoint.getLongitude());
173          this.callback(return_location, this);
174          break;
175          default:
176          alert(api + ' not supported by mapstraction-geocoder');
177          break;
178      }
179  }