Permalink
Browse files

Refined geocoder implementation

  • Loading branch information...
1 parent 1d25e4e commit 05327e48c96f31f4e20f12b6abe144822f38f038 @dezfowler dezfowler committed Aug 17, 2010
View
@@ -55,6 +55,7 @@
<arg value="tools/jsdoc-toolkit/app/run.js"/>
<arg value="${build.dir}/mxn.js"/>
<arg value="${build.dir}/mxn.core.js"/>
+ <arg value="${build.dir}/mxn.geocoder.js"/>
<arg value="-c=tools/jsdoc-toolkit/mxn.conf"/>
<arg value="-d=${build.dir}/docs"/>
</java>
@@ -1,22 +1,30 @@
-mxn.register('{{api_id}}', {
+mxn.register('{{api_id}}', {
-MapstractionGeocoder: {
+Geocoder: {
init: function(element, api) {
@brunob

brunob Oct 14, 2010

Contributor

Since http://github.com/mapstraction/mxn/commit/05327e48c96f31f4e20f12b6abe144822f38f038#L3R9

init: function(element, api)

Not sure but, should be replaced (?) by this

init: function(api, callback)
@dezfowler

dezfowler Oct 21, 2010

Owner

Good spot, indeed it should however you can access these variables through the properties this.asp and this.callback so it may make more sense to remove the arguments altogether.

@brunob

brunob Oct 21, 2010

Contributor

ok i've commmited this on my repo, it will appear on my pull req.

var me = this;
// TODO: Add provider code
},
+
geocode: function(address){
- var return_location = {};
- var mapstraction_geodocer = this;
+ var mapstraction_geocoder = this;
// TODO: Add provider code
},
- geocode_callback: function(response, mapstraction_geocoder){
+
+ geocode_callback: function(response){
var return_location = {};
// TODO: Add provider code
+ //return_location.street = '';
+ //return_location.locality = '';
+ //return_location.region = '';
+ //return_location.country = '';
+ //return_location.point = new mxn.LatLonPoint(...);
+
+ this.callback(return_location);
}
}
-})
+});
@@ -1,15 +1,16 @@
mxn.register('cartociudad', {
-MapstractionGeocoder: {
+Geocoder: {
init: function(element, api) {
@brunob

brunob Oct 14, 2010

Contributor

Since http://github.com/mapstraction/mxn/commit/05327e48c96f31f4e20f12b6abe144822f38f038#L3R9

init: function(element, api)

Not sure but, should be replaced (?) by this

init: function(api, callback)
this.geocoders[api] = new metodosCartociudad();
},
+
geocode: function(address){
var return_location = {};
var mapstraction_geodocer = this;
- address.error=0; //creamos una variable para devolver errores
+ address.error = 0; //creamos una variable para devolver errores
this.geocoders[this.api].queryNomenclator(address);
@@ -18,13 +19,14 @@ MapstractionGeocoder: {
}
else {
this.geocoders[this.api].addressToMapstraction(address);
- this.callback(address, this);
+ this.callback(address);
}
},
+
geocode_callback: function(response, mapstraction_geocoder){
var return_location = {};
// TODO: Add provider code
}
}
-})
+});
View
@@ -1,86 +1,60 @@
(function(){
-/**
- * @exports mxn.util.$m as $m
- */
-var $m = mxn.util.$m;
/**
* Initialise our provider. This function should only be called
* from within mapstraction code, not exposed as part of the API.
* @private
*/
var init = function() {
- this.invoker.go('init', [ this.callback, this.api, this.error_callback ]);
+ this.invoker.go('init', [ this.api, this.callback, this.error_callback ]);
};
+
/**
- * MapstractionGeocoder instantiates a geocoder with some API choice
- * @param {Function} callback The function to call when a geocode request returns (function(waypoint))
+ * Geocoder instantiates a geocoder with some API choice
+ * @name mxn.Geocoder
+ * @constructor
* @param {String} api The API to use, currently only 'mapquest' is supported
+ * @param {Function} callback The function to call when a geocode request returns (function(waypoint))
* @param {Function} error_callback The optional function to call when a geocode request fails
- * @constructor
+ * @exports Geocoder as mxn.Geocoder
*/
-var MapstractionGeocoder = mxn.MapstractionGeocoder = function (callback, api, error_callback) {
- this.api = api;
- this.callback = callback;
- this.geocoders = new Object();
- if(error_callback == null) {
- this.error_callback = this.geocode_error
- } else {
- this.error_callback = error_callback;
- }
-
- // set up our invoker for calling API methods
- this.invoker = new mxn.Invoker(this, 'MapstractionGeocoder', function(){ return this.api; });
- init.apply(this);
-}
-
-mxn.addProxyMethods(MapstractionGeocoder, [
-
- /*
- * Implements the geocoding process
- */
- 'geocode',
-
- /*
- * Implements the callback fucnction in geocoding process
- */
- 'geocode_callback',
-
-])
+var Geocoder = mxn.Geocoder = function (api, callback, error_callback) {
+ this.api = api;
+ this.geocoders = {};
+ this.callback = callback;
+ this.error_callback = error_callback || function(){};
+
+ // set up our invoker for calling API methods
+ this.invoker = new mxn.Invoker(this, 'Geocoder', function(){ return this.api; });
+ init.apply(this);
+};
-/**
- * Performs a geocoding and then calls the specified callback function with the location
- * @param {Object} address The address object to geocode
- */
-MapstractionGeocoder.prototype.geocode = function(address) {
- this.invoker.go('geocode',arguments);
-}
+mxn.addProxyMethods(Geocoder, [
+
+ /**
+ * Geocodes the provided address.
+ * @name mxn.Geocoder#geocode
+ * @function
+ * @param {Object} address Address hash, keys are: street, locality, region, country.
+ */
+ 'geocode',
+
+ 'geocode_callback'
-/**
- * Default handler for geocode request completion
- */
-MapstractionGeocoder.prototype.geocode_callback = function(response, mapstraction_geocoder) {
- this.invoker.go('geocode_callback',arguments);
-}
+]);
/**
- * Change the Routing API to use
+ * Change the geocoding API in use
+ * @name mxn.Geocoder#swap
* @param {String} api The API to swap to
*/
-MapstractionGeocoder.prototype.swap = function(api) {
- if (this.api == api) { return; }
+Geocoder.prototype.swap = function(api) {
+ if (this.api == api) { return; }
- this.api = api;
- if (this.geocoders[this.api] == undefined) {
- init.apply(this);
- }
-}
-
-/**
- * Default Geocode error function
- */
-MapstractionGeocoder.prototype.geocode_error = function(response) {
- alert("Sorry, we were unable to geocode that address");
-}
+ this.api = api;
+ if (!this.geocoders.hasOwnProperty(this.api)) {
+ init.apply(this);
+ }
+};
})();
@@ -1,49 +1,56 @@
mxn.register('google', {
-MapstractionGeocoder: {
+Geocoder: {
init: function(element, api) {
- var me = this;
- me.geocoders[api] = new GClientGeocoder();
+ this.geocoders[api] = new GClientGeocoder();
},
- geocode: function(address){
- var return_location = {};
- var mapstraction_geocoder = this;
- if (address.address == null || address.address == "")
- address.address = address.street + ", " + address.locality + ", " + address.region + ", " + address.country
- this.geocoders[this.api].getLocations(address.address, function(response) { mapstraction_geocoder.geocode_callback(response, mapstraction_geocoder); });
- },
- geocode_callback: function(response, mapstraction_geocoder){
- var return_location = {};
+ geocode: function(address){
+ var me = this;
+
+ if (!address.hasOwnProperty('address') || address.address === null || address.address === '') {
+ address.address = [ address.street, address.locality, address.region, address.country ].join(', ');
+ }
- if (!response || response.Status.code != 200) {
- mapstraction_geocoder.error_callback(response);
- } else {
- return_location.street = "";
- return_location.locality = "";
- return_location.region = "";
- return_location.country = "";
+ this.geocoders[this.api].getLocations(address.address, function(response) {
+ me.geocode_callback(response);
+ });
+ },
+
+ geocode_callback: function(response){
+ var return_location = {};
+
+ if (typeof(response) === 'undefined' || !response.hasOwnProperty('Status') || response.Status.code != 200) {
+ this.error_callback(response);
+ }
+ else {
+ return_location.street = '';
+ return_location.locality = '';
+ return_location.region = '';
+ return_location.country = '';
- var place = response.Placemark[0];
- if(place.AddressDetails.Country.AdministrativeArea != null) {
- return_location.region = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
-
- if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea != null) {
- if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality != null) {
- return_location.locality = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
- if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare != null)
- return_location.street = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
- }
-
- }
-
- }
- return_location.country = place.AddressDetails.Country.CountryNameCode;
- return_location.point = new mxn.LatLonPoint(place.Point.coordinates[1],
- place.Point.coordinates[0]);
- mapstraction_geocoder.callback(return_location);
- }
+ var place = response.Placemark[0];
+ var working = place.AddressDetails.Country.AdministrativeArea;
+ if(working !== null) {
+ return_location.region = working.AdministrativeAreaName;
+ if(working.SubAdministrativeArea !== null) {
+ working = working.SubAdministrativeArea;
+ if(working.Locality !== null) {
+ working = working.Locality;
+ return_location.locality = working.LocalityName;
+ if(working.Thoroughfare !== null) {
+ return_location.street = working.Thoroughfare.ThoroughfareName;
+ }
+ }
+ }
+ }
+
+ return_location.country = place.AddressDetails.Country.CountryNameCode;
+ return_location.point = new mxn.LatLonPoint(place.Point.coordinates[1], place.Point.coordinates[0]);
+
+ this.callback(return_location);
+ }
}
}
-})
+});
@@ -1,27 +1,27 @@
mxn.register('mapquest', {
-MapstractionGeocoder: {
+Geocoder: {
init: function(element, api) {
- var me = this;
-
//set up the connection to the geocode server
var proxyServerName = "";
var proxyServerPort = "";
- var ProxyServerPath = "mapquest_proxy/JSReqHandler.php";
-
+ var proxyServerPath = "mapquest_proxy/JSReqHandler.php";
var serverName = "geocode.access.mapquest.com";
var serverPort = "80";
var serverPath = "mq";
+
this.geocoders[api] = new MQExec(serverName, serverPath, serverPort, proxyServerName,
- ProxyServerPath, proxyServerPort );
+ proxyServerPath, proxyServerPort );
},
+
geocode: function(address){
var return_location = {};
var mapstraction_geodocer = this;
var mqaddress = new MQAddress();
var gaCollection = new MQLocationCollection("MQGeoAddress");
+
//populate the address object with the information from the form
mqaddress.setStreet(address.street);
mqaddress.setCity(address.locality);
@@ -32,17 +32,18 @@ MapstractionGeocoder: {
this.geocoders[this.api].geocode(mqaddress, gaCollection);
var geoAddr = gaCollection.get(0);
var mqpoint = geoAddr.getMQLatLng();
+
return_location.street = geoAddr.getStreet();
return_location.locality = geoAddr.getCity();
return_location.region = geoAddr.getState();
return_location.country = geoAddr.getCountry();
return_location.point = new LatLonPoint(mqpoint.getLatitude(), mqpoint.getLongitude());
- this.callback(return_location, this);
- },
- geocode_callback: function(response, mapstraction_geocoder){
- var return_location = {};
- // TODO: Add provider code
+ this.callback(return_location);
+ },
+
+ geocode_callback: function(response){
+ throw 'Not used';
}
}
-})
+});

0 comments on commit 05327e4

Please sign in to comment.