-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented geocode module for microsoft7 provider
added api key to geocode constructor geocode function now accepts a string literal address in addition to address object and latlonpoint
- Loading branch information
Greg Woods
committed
Nov 7, 2011
1 parent
26a38be
commit 6b0c0f8
Showing
3 changed files
with
160 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,87 @@ | ||
mxn.register('googlev3', { | ||
mxn.register('googlev3', { | ||
|
||
Geocoder: { | ||
init: function() { | ||
this.geocoders[this.api] = new google.maps.Geocoder(); | ||
}, | ||
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 (address.hasOwnProperty('lat') && address.hasOwnProperty('lon')) { | ||
var latlon = address.toProprietary(this.api); | ||
this.geocoders[this.api].geocode({'latLng': latlon }, function(results, status) { | ||
me.geocode_callback(results, status); | ||
}); | ||
} else { | ||
this.geocoders[this.api].geocode({'address': address.address }, function(results, status) { | ||
me.geocode_callback(results, status); | ||
}); | ||
} | ||
}, | ||
geocode_callback: function(results, status){ | ||
var return_location = {}; | ||
init: function() { | ||
this.geocoders[this.api] = new google.maps.Geocoder(); | ||
}, | ||
geocode: function(query){ | ||
var me = this; | ||
var geocode_request_object = {}; | ||
if(typeof(query) == 'object'){ | ||
// query is a LatLonPoint object (reverse geocode) | ||
if (query.hasOwnProperty('lat') && query.hasOwnProperty('lon')) { | ||
geocode_request_object.latLng = query.toProprietary(this.api); | ||
} | ||
// query is an address object | ||
else{ | ||
geocode_request_object.address = [ query.street, query.locality, query.region, query.country ].join(', '); | ||
} | ||
} | ||
// query is an address string | ||
else{ | ||
geocode_request_object.address = query; | ||
} | ||
this.geocoders[this.api].geocode(geocode_request_object, function(results, status) { | ||
me.geocode_callback(results, status); | ||
}); | ||
}, | ||
|
||
if (status != google.maps.GeocoderStatus.OK) { | ||
this.error_callback(status); | ||
} | ||
else { | ||
return_location.street = ''; | ||
return_location.locality = ''; | ||
return_location.postcode = ''; | ||
return_location.region = ''; | ||
return_location.country = ''; | ||
geocode_callback: function(results, status){ | ||
var return_location = {}; | ||
|
||
var place = results[0]; | ||
var streetparts = []; | ||
|
||
for (var i = 0; i < place.address_components.length; i++) { | ||
var addressComponent = place.address_components[i]; | ||
for (var j = 0; j < addressComponent.types.length; j++) { | ||
var componentType = addressComponent.types[j]; | ||
switch (componentType) { | ||
case 'country': | ||
return_location.country = addressComponent.long_name; | ||
break; | ||
case 'administrative_area_level_1': | ||
return_location.region = addressComponent.long_name; | ||
break; | ||
case 'locality': | ||
return_location.locality = addressComponent.long_name; | ||
break; | ||
case 'street_address': | ||
return_location.street = addressComponent.long_name; | ||
break; | ||
case 'postal_code': | ||
return_location.postcode = addressComponent.long_name; | ||
break; | ||
case 'street_number': | ||
streetparts.unshift(addressComponent.long_name); | ||
break; | ||
case 'route': | ||
streetparts.push(addressComponent.long_name); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (return_location.street === '' && streetparts.length > 0) { | ||
return_location.street = streetparts.join(' '); | ||
} | ||
|
||
return_location.point = new mxn.LatLonPoint(place.geometry.location.lat(), place.geometry.location.lng()); | ||
|
||
this.callback(return_location); | ||
} | ||
} | ||
if (status != google.maps.GeocoderStatus.OK) { | ||
this.error_callback(status); | ||
} | ||
else { | ||
return_location.street = ''; | ||
return_location.locality = ''; | ||
return_location.postcode = ''; | ||
return_location.region = ''; | ||
return_location.country = ''; | ||
|
||
var place = results[0]; | ||
var streetparts = []; | ||
|
||
for (var i = 0; i < place.address_components.length; i++) { | ||
var addressComponent = place.address_components[i]; | ||
for (var j = 0; j < addressComponent.types.length; j++) { | ||
var componentType = addressComponent.types[j]; | ||
switch (componentType) { | ||
case 'country': | ||
return_location.country = addressComponent.long_name; | ||
break; | ||
case 'administrative_area_level_1': | ||
return_location.region = addressComponent.long_name; | ||
break; | ||
case 'locality': | ||
return_location.locality = addressComponent.long_name; | ||
break; | ||
case 'street_address': | ||
return_location.street = addressComponent.long_name; | ||
break; | ||
case 'postal_code': | ||
return_location.postcode = addressComponent.long_name; | ||
break; | ||
case 'street_number': | ||
streetparts.unshift(addressComponent.long_name); | ||
break; | ||
case 'route': | ||
streetparts.push(addressComponent.long_name); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (return_location.street === '' && streetparts.length > 0) { | ||
return_location.street = streetparts.join(' '); | ||
} | ||
|
||
return_location.point = new mxn.LatLonPoint(place.geometry.location.lat(), place.geometry.location.lng()); | ||
|
||
this.callback(return_location); | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
mxn.register('microsoft7', { | ||
Geocoder: { | ||
|
||
init: function(){ | ||
|
||
}, | ||
|
||
geocode: function(query){ | ||
var self = this; | ||
var _address = ''; | ||
var is_reverse = false; | ||
if(typeof(query) == 'object'){ | ||
// query is a LatLonPoint object (reverse geocode) | ||
if (query.hasOwnProperty('lat') && query.hasOwnProperty('lon')) { | ||
_address = query.lat + ',' + query.lon; | ||
} | ||
// query is an address object | ||
else{ | ||
_address = [ query.street, query.locality, query.region, query.country ].join(', '); | ||
} | ||
} | ||
// query is an address string | ||
else{ | ||
_address = query; | ||
} | ||
jsonp_callback_context = this; | ||
var searchRequest = 'http://dev.virtualearth.net/REST/v1/Locations/' + _address + '?output=json&jsonp=jsonp_callback_context.geocode_callback&key=' + this.key; | ||
var mapscript = document.createElement('script'); | ||
mapscript.type = 'text/javascript'; | ||
mapscript.src = searchRequest; | ||
document.body.appendChild(mapscript); | ||
}, | ||
|
||
geocode_callback: function(results){ | ||
if(results.statusDescription != 'OK'){ | ||
this.error_callback(results.statusDescription); | ||
} | ||
else{ | ||
var topResult = results.resourceSets[0].resources[0]; | ||
var return_location = { | ||
street: topResult.address.addressLine, | ||
locality: topResult.address.locality, | ||
postcode: topResult.address.postalCode, | ||
region: topResult.address.adminDistrict, | ||
country: topResult.address.countryRegion, | ||
point: new mxn.LatLonPoint(topResult.point.coordinates[0], topResult.point.coordinates[1]) | ||
}; | ||
this.callback(return_location); | ||
} | ||
} | ||
} | ||
}); |