mxn.google.geocoder.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (c) 2007, Andrew Turner
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  6. * 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.
  7. * 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.
  8. 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.
  9. */
  10. // Use http://jsdoc.sourceforge.net/ to generate documentation
  11. // TODO: add reverse geocoding support
  12. /**
  13. * MapstractionGeocoder instantiates a geocoder with some API choice
  14. * @param {Function} callback The function to call when a geocode request returns (function(waypoint))
  15. * @param {String} api The API to use, currently only 'mapquest' is supported
  16. * @param {Function} error_callback The optional function to call when a geocode request fails
  17. * @constructor
  18. */
  19. function MapstractionGeocoder(callback, api, error_callback) {
  20. this.api = api;
  21. this.callback = callback;
  22. this.geocoders = new Object();
  23. if(error_callback == null) {
  24. this.error_callback = this.geocode_error
  25. } else {
  26. this.error_callback = error_callback;
  27. }
  28. // This is so that it is easy to tell which revision of this file
  29. // has been copied into other projects.
  30. this.svn_revision_string = '$Revision: 107 $';
  31. this.addAPI(api);
  32. }
  33. /**
  34. * Internal function to actually set the router specific parameters
  35. */
  36. MapstractionGeocoder.prototype.addAPI = function(api) {
  37. me = this;
  38. switch (api) {
  39. case 'google':
  40. this.geocoders[api] = new GClientGeocoder();
  41. break;
  42. case 'mapquest':
  43. //set up the connection to the geocode server
  44. var proxyServerName = "";
  45. var proxyServerPort = "";
  46. var ProxyServerPath = "mapquest_proxy/JSReqHandler.php";
  47. var serverName = "geocode.access.mapquest.com";
  48. var serverPort = "80";
  49. var serverPath = "mq";
  50. this.geocoders[api] = new MQExec(serverName, serverPath, serverPort, proxyServerName,
  51. ProxyServerPath, proxyServerPort );
  52. break;
  53. default:
  54. alert(api + ' not supported by mapstraction-geocoder');
  55. }
  56. }
  57. /**
  58. * Change the Routing API to use
  59. * @param {String} api The API to swap to
  60. */
  61. MapstractionGeocoder.prototype.swap = function(api) {
  62. if (this.api == api) { return; }
  63. this.api = api;
  64. if (this.geocoders[this.api] == undefined) {
  65. this.addAPI($(element),api);
  66. }
  67. }
  68. /**
  69. * Default Geocode error function
  70. */
  71. MapstractionGeocoder.prototype.geocode_error = function(response) {
  72. alert("Sorry, we were unable to geocode that address");
  73. }
  74. /**
  75. * Default handler for geocode request completion
  76. */
  77. MapstractionGeocoder.prototype.geocode_callback = function(response, mapstraction_geocoder) {
  78. var return_location = new Object();
  79. // TODO: what if the api is switched during a geocode request?
  80. // TODO: provide an option error callback
  81. switch (mapstraction_geocoder.api) {
  82. case 'google':
  83. if (!response || response.Status.code != 200) {
  84. mapstraction_geocoder.error_callback(response);
  85. } else {
  86. return_location.street = "";
  87. return_location.locality = "";
  88. return_location.region = "";
  89. return_location.country = "";
  90. var place = response.Placemark[0];
  91. if(place.AddressDetails.Country.AdministrativeArea != null) {
  92. return_location.region = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
  93. if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea != null) {
  94. if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality != null) {
  95. return_location.locality = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
  96. if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare != null)
  97. return_location.street = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
  98. }
  99. }
  100. }
  101. return_location.country = place.AddressDetails.Country.CountryNameCode;
  102. return_location.address = place.address;
  103. return_location.point = new mxn.LatLonPoint(place.Point.coordinates[1],
  104. place.Point.coordinates[0]);
  105. mapstraction_geocoder.callback(return_location);
  106. }
  107. break;
  108. case 'mapquest':
  109. break;
  110. }
  111. }
  112. /**
  113. * Performs a geocoding and then calls the specified callback function with the location
  114. * @param {Object} address The address object to geocode
  115. */
  116. MapstractionGeocoder.prototype.geocode = function(address) {
  117. var return_location = new Object();
  118. // temporary variable for later using in function closure
  119. var mapstraction_geocoder = this;
  120. switch (this.api) {
  121. case 'google':
  122. if (address.address == null || address.address == "")
  123. address.address = address.street + ", " + address.locality + ", " + address.region + ", " + address.country
  124. this.geocoders[this.api].getLocations(address.address, function(response) { mapstraction_geocoder.geocode_callback(response, mapstraction_geocoder); });
  125. break;
  126. case 'mapquest':
  127. var mqaddress = new MQAddress();
  128. var gaCollection = new MQLocationCollection("MQGeoAddress");
  129. //populate the address object with the information from the form
  130. mqaddress.setStreet(address.street);
  131. mqaddress.setCity(address.locality);
  132. mqaddress.setState(address.region);
  133. mqaddress.setPostalCode(address.postalcode);
  134. mqaddress.setCountry(address.country);
  135. this.geocoders[this.api].geocode(mqaddress, gaCollection);
  136. var geoAddr = gaCollection.get(0);
  137. var mqpoint = geoAddr.getMQLatLng();
  138. return_location.street = geoAddr.getStreet();
  139. return_location.locality = geoAddr.getCity();
  140. return_location.region = geoAddr.getState();
  141. return_location.country = geoAddr.getCountry();
  142. return_location.point = new mxn.LatLonPoint(mqpoint.getLatitude(), mqpoint.getLongitude());
  143. this.callback(return_location, this);
  144. break;
  145. default:
  146. alert(api + ' not supported by mapstraction-geocoder');
  147. break;
  148. }
  149. }