Skip to content

Commit

Permalink
Refined geocoder implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dezfowler committed Aug 17, 2010
1 parent 1d25e4e commit 05327e4
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 125 deletions.
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
20 changes: 14 additions & 6 deletions source/mxn.(provider).geocoder.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
mxn.register('{{api_id}}', {
mxn.register('{{api_id}}', {

MapstractionGeocoder: {
Geocoder: {

init: function(element, api) {

This comment has been minimized.

Copy link
@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 comment has been minimized.

Copy link
@dezfowler

dezfowler Oct 21, 2010

Author Member

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.

This comment has been minimized.

Copy link
@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);
}
}
})
});
10 changes: 6 additions & 4 deletions source/mxn.cartociudad.geocoder.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
mxn.register('cartociudad', {

MapstractionGeocoder: {
Geocoder: {

init: function(element, api) {

This comment has been minimized.

Copy link
@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);

Expand All @@ -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
}
}
})
});
102 changes: 38 additions & 64 deletions source/mxn.geocoder.js
Original file line number Diff line number Diff line change
@@ -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);
}
};

})();
85 changes: 46 additions & 39 deletions source/mxn.google.geocoder.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
})
});
25 changes: 13 additions & 12 deletions source/mxn.mapquest.geocoder.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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.