Skip to content

Commit 9cc8416

Browse files
committed
supported bidirectional address search
1 parent 958e4a0 commit 9cc8416

File tree

1 file changed

+67
-49
lines changed

1 file changed

+67
-49
lines changed

assets/javascripts/app.js

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -605,57 +605,43 @@ var App = (function ($, publ) {
605605
);
606606

607607
$("button.btn-geocode").on("click", function(evt) {
608-
if (vector.getSource().getFeatures().length > 0) {
609-
var coords = null
610-
vector.getSource().getFeatures().forEach(function (feature) {
611-
// Todo: only works with point geometries for now for the last geometry
612-
coords = feature.getGeometry().getCoordinates();
613-
});
614-
coords = ol.proj.transform(coords,'EPSG:3857','EPSG:4326')
615-
$.getJSON("https://***REMOVED***/reversegeocode/json/" + coords.join(",") + ",1000", function(data) {
616-
var addressInput = $("#issue-form #attributes label:contains('" + defaults.geocoderAddress + "')").parent("p").children("input");
617-
var districtInput = $("#issue-form #attributes label:contains('" + defaults.geocoderDistrict + "')").parent("p").children("input");
618-
var foundDistrict = false;
619-
if (data.result.address) {
620-
addressInput.val(data.result.address);
621-
if (districtInput && data.result.shikuchoson) {
622-
var regexp = /^(?:\S+)?(\S+)$/g;
623-
var match = regexp.exec(data.result.shikuchoson);
624-
if (match && match.length === 2) {
625-
districtInput.val(match[1]);
626-
foundDistrict = true;
608+
// Geocode address and add/update icon on map
609+
if ($("button.btn-geocode").prev("input").val() != "") {
610+
var address = $("button.btn-geocode").prev("input").val()
611+
$.getJSON("https://***REMOVED***/geocode/json/" + encodeURIComponent(address), function(data) {
612+
if (data.result.code >= 0 && data.result.coordinates) {
613+
var geom = new ol.geom.Point(
614+
ol.proj.fromLonLat(Object.values(data.result.coordinates), 'EPSG:3857','EPSG:4326')
615+
)
616+
var features = vector.getSource().getFeatures();
617+
if (features.length > 0) {
618+
features[features.length - 1].setGeometry(geom);
619+
} else {
620+
var feature = new ol.Feature({geometry: geom});
621+
vector.getSource().getFeatures().push(feature);
622+
}
623+
publ.updateForm(vector.getSource().getFeatures());
624+
publ.zoomToExtent(true);
625+
626+
var districtInput = $("#issue-form #attributes label:contains('" + defaults.geocoderDistrict + "')").parent("p").children("input");
627+
if (districtInput.length > 0) {
628+
var foundDistrict = false;
629+
if (data.result.shikuchoson) {
630+
var regexp = /^(?:\S+)?(\S+)$/g;
631+
var match = regexp.exec(data.result.shikuchoson);
632+
if (match && match.length === 2) {
633+
districtInput.val(match[1]);
634+
foundDistrict = true;
635+
}
636+
}
637+
if (!foundDistrict) {
638+
districtInput.val("");
627639
}
628640
}
629641
}
630-
else {
631-
addressInput.val("---");
632-
}
633-
if (!foundDistrict) {
634-
districtInput.val("");
635-
}
636-
});
637-
}else{
638-
// GEOCODE ADDRESS AND ADD ICON TO MAP
639-
if ($("button.btn-geocode").prev("input").val() != ""){
640-
coords = $("button.btn-geocode").prev("input").val()
641-
$.getJSON("https://***REMOVED***/geocode/json/" + coords, function(data) {
642-
if(data.result.coordinates){
643-
var feature = new ol.Feature({
644-
geometry: new ol.geom.Point(
645-
ol.proj.fromLonLat(Object.values(data.result.coordinates), 'EPSG:3857','EPSG:4326')
646-
)
647-
});
648-
vector.getSource().getFeatures().push(feature)
649-
publ.updateForm(vector.getSource().getFeatures());
650-
publ.zoomToExtent();
651-
}
652-
})
653-
}else{
654-
alert("Address is empty!")
655-
}
642+
})
656643
}
657644
});
658-
659645
}
660646

661647
if ( $("#issue-form #attributes button.btn-parksearch").length == 0 ) {
@@ -741,7 +727,7 @@ var App = (function ($, publ) {
741727
});
742728

743729
modify.on('modifyend', function(evt) {
744-
this.updateForm(evt.features.getArray());
730+
this.updateForm(evt.features.getArray(), true);
745731
}, publ);
746732

747733
map.addInteraction(modify);
@@ -765,7 +751,7 @@ var App = (function ($, publ) {
765751

766752
draw.on('drawend', function(evt) {
767753
(vector.getSource()).clear();
768-
publ.updateForm([evt.feature]);
754+
publ.updateForm([evt.feature], true);
769755
});
770756

771757
var control = new ol.control.Toggle({
@@ -855,13 +841,45 @@ var App = (function ($, publ) {
855841
/**
856842
*
857843
*/
858-
publ.updateForm = function (features) {
844+
publ.updateForm = function (features, updateAddressFlag) {
859845
var writer = new ol.format.GeoJSON();
860846
var geojson = JSON.parse(writer.writeFeatures(features, {
861847
featureProjection: 'EPSG:3857',
862848
dataProjection: 'EPSG:4326'
863849
}));
864850
$("#geom").val(JSON.stringify(geojson.features[0]));
851+
852+
if (updateAddressFlag && features && features.length > 0) {
853+
var addressInput = $("#issue-form #attributes label:contains('" + defaults.geocoderAddress + "')").parent("p").children("input");
854+
if (addressInput.length > 0) {
855+
// Todo: only works with point geometries for now for the last geometry
856+
var coords = features[features.length - 1].getGeometry().getCoordinates();
857+
coords = ol.proj.transform(coords,'EPSG:3857','EPSG:4326')
858+
$.getJSON("https://***REMOVED***/reversegeocode/json/" + coords.join(",") + ",1000", function(data) {
859+
var districtInput = $("#issue-form #attributes label:contains('" + defaults.geocoderDistrict + "')").parent("p").children("input");
860+
var foundDistrict = false;
861+
if (data.result.code >= 0 && data.result.address) {
862+
addressInput.val(data.result.address);
863+
if (districtInput.length > 0 && data.result.shikuchoson) {
864+
var regexp = /^(?:\S+)?(\S+)$/g;
865+
var match = regexp.exec(data.result.shikuchoson);
866+
if (match && match.length === 2) {
867+
districtInput.val(match[1]);
868+
foundDistrict = true;
869+
}
870+
}
871+
}
872+
else {
873+
addressInput.val("---");
874+
}
875+
if (!foundDistrict) {
876+
if (districtInput.length > 0) {
877+
districtInput.val("");
878+
}
879+
}
880+
});
881+
}
882+
}
865883
};
866884

867885
publ.getScale = function () {

0 commit comments

Comments
 (0)