Skip to content

Commit

Permalink
dotevo#31 getting addresses from Nominatim
Browse files Browse the repository at this point in the history
When a POI does not have street name and house number, the address is
obtained from Nominatim
  • Loading branch information
edewaele committed Jul 4, 2015
1 parent 07ebfec commit be71ece
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
6 changes: 4 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,12 @@ var zoomControl = L.control.zoom({
map.on('locationerror', onLocationError);

map.on('moveend', nominatimQuery);
map.on('popupopen', function() {
map.on('popupopen', function(e) {
var href=$("#plusone-div").data("href");
var size=$("#plusone-div").data("size");
gapi.plusone.render("plusone-div",{'size':size,'href':href});
//gapi.plusone.render("plusone-div",{'size':size,'href':href});
// we make sure that a proper address is displayed
e.popup._source.el.fetchAddress();
});
n=true;
var href=$("#main-plus").data("href");
Expand Down
49 changes: 47 additions & 2 deletions js/poi.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ POI.prototype.getInfoBox = function(){
//name
content+='<h4><a href=\'http://osm24.eu/index.php?id='+this.element.id+'#!18/'+this.element.lat+'/'+this.element.lon+'/\'>' +(this.getName()?this.getName():"---")+'</a> <div id="plusone-div" data-size="small" data-href=\'http://osm24.eu/index.php?id='+this.element.id+'#!18/'+this.element.lat+'/'+this.element.lon+'/\'></div></h4>';
//addr
content+='<small>'+((this.element.tags.hasOwnProperty("addr:city")) ? this.element.tags["addr:city"]+', ' : "")+((this.element.tags.hasOwnProperty("addr:street")) ? this.element.tags["addr:street"]+', ' : "")+((this.element.tags.hasOwnProperty("addr:housenumber")) ? this.element.tags["addr:housenumber"]+', ' : "")+'</small>';

if(this.element.tags.hasOwnProperty("addr:housenumber") && this.element.tags.hasOwnProperty("addr:street"))
content+='<small>'+((this.element.tags.hasOwnProperty("addr:city")) ? this.element.tags["addr:city"]+', ' : "")+this.element.tags["addr:street"]+', ' +this.element.tags["addr:housenumber"]+'</small>';
else
content+='<div id="'+this.element.type+this.element.id+'">'+lang_loading+'</div>';
//net
content+='<div>';
content += this.__genItems({tag:['contact:email','email'],icon:'glyphicon glyphicon-envelope',href:'mailto:'});
Expand Down Expand Up @@ -284,3 +286,46 @@ POI.prototype.updateShadow = function (now){
return false;
return true;
};

/*
Determine the address of a POI, if it is unknown
*/
POI.prototype.fetchAddress = function(){
var typeTable = {"node":"N","way":"W","relation":"R"};
var addressDiv = this.element.type+this.element.id;
// if the address has not been determined yet
if($("#"+addressDiv).html() == lang_loading)
{
var requestData;
if(this.element.tags.hasOwnProperty("addr:housenumber"))
{
if(this.element.type)
requestData = {osm_type:typeTable[this.element.type],osm_id:this.element.id};
else
// way objects have no key type, no idea why
requestData = {osm_type:this.element.id[0].toUpperCase(),osm_id:this.element.id.substr(1)};
}
else
requestData = {lon:this.element.lon,lat:this.element.lat};

$.ajax({
url: 'nominatim_reverse_proxy.php',
dataType: "json",
data: requestData,
number : this.element.tags["addr:housenumber"],
addressDiv : addressDiv,
success: function(nominatim){
var wayName = nominatim.address.road;
if(!wayName)wayName = nominatim.address.pedestrian;
if(!wayName)wayName = nominatim.address.footway;
var cityName = nominatim.address.city;
if(!cityName)cityName = nominatim.address.town;
if(!cityName)cityName = nominatim.address.village;
var displayedAddress = cityName?cityName:"";
displayedAddress += (wayName?", "+wayName:"");
displayedAddress += (this.number?", "+this.number:"");
$("#"+this.addressDiv).html('<small>'+displayedAddress+'</small>');
}
});
}
};
6 changes: 6 additions & 0 deletions nominatim_reverse_proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
// This file performs a reverse geocoding request with nominatim API
header('Content-Type: application/json');
$_GET["format"] = "json";
echo file_get_contents('http://nominatim.openstreetmap.org/reverse?'.http_build_query($_GET));
?>

0 comments on commit be71ece

Please sign in to comment.