2 Copyright (c) 2007, Andrew Turner
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
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.
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.
15 // Use http://jsdoc.sourceforge.net/ to generate documentation
17 // TODO: add reverse geocoding support
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
26 function MapstractionGeocoder(callback, api, error_callback) {
28 this.callback = callback;
29 this.geocoders = new Object();
30 if(error_callback == null) {
31 this.error_callback = this.geocode_error
33 this.error_callback = error_callback;
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 $';
46 * Internal function to actually set the router specific parameters
48 MapstractionGeocoder.prototype.addAPI = function(api) {
53 this.geocoders[api] = new GClientGeocoder();
56 //set up the connection to the geocode server
57 var proxyServerName = "";
58 var proxyServerPort = "";
59 var ProxyServerPath = "mapquest_proxy/JSReqHandler.php";
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 );
69 alert(api + ' not supported by mapstraction-geocoder');
73 * Change the Routing API to use
74 * @param {String} api The API to swap to
76 MapstractionGeocoder.prototype.swap = function(api) {
77 if (this.api == api) { return; }
80 if (this.geocoders[this.api] == undefined) {
81 this.addAPI($(element),api);
86 * Default Geocode error function
88 MapstractionGeocoder.prototype.geocode_error = function(response) {
89 alert("Sorry, we were unable to geocode that address");
93 * Default handler for geocode request completion
95 MapstractionGeocoder.prototype.geocode_callback = function(response, mapstraction_geocoder) {
96 var return_location = new Object();
98 // TODO: what if the api is switched during a geocode request?
99 // TODO: provide an option error callback
100 switch (mapstraction_geocoder.api) {
102 if (!response || response.Status.code != 200) {
103 mapstraction_geocoder.error_callback(response);
105 return_location.street = "";
106 return_location.locality = "";
107 return_location.region = "";
108 return_location.country = "";
110 var place = response.Placemark[0];
111 if(place.AddressDetails.Country.AdministrativeArea != null) {
112 return_location.region = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
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;
118 if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare != null)
119 return_location.street = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
125 return_location.country = place.AddressDetails.Country.CountryNameCode;
126 return_location.address = place.address;
128 return_location.point = new mxn.LatLonPoint(place.Point.coordinates[1],
129 place.Point.coordinates[0]);
130 mapstraction_geocoder.callback(return_location);
140 * Performs a geocoding and then calls the specified callback function with the location
141 * @param {Object} address The address object to geocode
143 MapstractionGeocoder.prototype.geocode = function(address) {
144 var return_location = new Object();
146 // temporary variable for later using in function closure
147 var mapstraction_geocoder = this;
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); });
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);
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);
176 alert(api + ' not supported by mapstraction-geocoder');