Skip to content

Commit 1c1387f

Browse files
committed
Merge branch 'develop' into feature/131_focus_scroll
2 parents 2c6925d + fcdb32f commit 1c1387f

File tree

1 file changed

+67
-30
lines changed

1 file changed

+67
-30
lines changed

assets/javascripts/app.js

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -616,38 +616,43 @@ var App = (function ($, publ) {
616616
);
617617

618618
$("button.btn-geocode").on("click", function(evt) {
619-
if (vector.getSource().getFeatures().length > 0) {
620-
var coords = null
621-
vector.getSource().getFeatures().forEach(function (feature) {
622-
// Todo: only works with point geometries for now for the last geometry
623-
coords = feature.getGeometry().getCoordinates();
624-
});
625-
coords = ol.proj.transform(coords,'EPSG:3857','EPSG:4326')
626-
$.getJSON("https://***REMOVED***/reversegeocode/json/" + coords.join(",") + ",1000", function(data) {
627-
var addressInput = $("#issue-form #attributes label:contains('" + defaults.geocoderAddress + "')").parent("p").children("input");
628-
var districtInput = $("#issue-form #attributes label:contains('" + defaults.geocoderDistrict + "')").parent("p").children("input");
629-
var foundDistrict = false;
630-
if (data.result.address) {
631-
addressInput.val(data.result.address);
632-
if (districtInput && data.result.shikuchoson) {
633-
var regexp = /^(?:\S+)?(\S+)$/g;
634-
var match = regexp.exec(data.result.shikuchoson);
635-
if (match && match.length === 2) {
636-
districtInput.val(match[1]);
637-
foundDistrict = true;
619+
// Geocode address and add/update icon on map
620+
if ($("button.btn-geocode").prev("input").val() != "") {
621+
var address = $("button.btn-geocode").prev("input").val()
622+
$.getJSON("https://***REMOVED***/geocode/json/" + encodeURIComponent(address), function(data) {
623+
if (data.result.code >= 0 && data.result.coordinates) {
624+
var geom = new ol.geom.Point(
625+
ol.proj.fromLonLat(Object.values(data.result.coordinates), 'EPSG:3857','EPSG:4326')
626+
)
627+
var features = vector.getSource().getFeatures();
628+
if (features.length > 0) {
629+
features[features.length - 1].setGeometry(geom);
630+
} else {
631+
var feature = new ol.Feature({geometry: geom});
632+
vector.getSource().getFeatures().push(feature);
633+
}
634+
publ.updateForm(vector.getSource().getFeatures());
635+
publ.zoomToExtent(true);
636+
637+
var districtInput = $("#issue-form #attributes label:contains('" + defaults.geocoderDistrict + "')").parent("p").children("input");
638+
if (districtInput.length > 0) {
639+
var foundDistrict = false;
640+
if (data.result.shikuchoson) {
641+
var regexp = /^(?:\S+)?(\S+)$/g;
642+
var match = regexp.exec(data.result.shikuchoson);
643+
if (match && match.length === 2) {
644+
districtInput.val(match[1]);
645+
foundDistrict = true;
646+
}
647+
}
648+
if (!foundDistrict) {
649+
districtInput.val("");
638650
}
639651
}
640652
}
641-
else {
642-
addressInput.val("---");
643-
}
644-
if (!foundDistrict) {
645-
districtInput.val("");
646-
}
647-
});
653+
})
648654
}
649655
});
650-
651656
}
652657

653658
if ( $("#issue-form #attributes button.btn-parksearch").length == 0 ) {
@@ -733,7 +738,7 @@ var App = (function ($, publ) {
733738
});
734739

735740
modify.on('modifyend', function(evt) {
736-
this.updateForm(evt.features.getArray());
741+
this.updateForm(evt.features.getArray(), true);
737742
}, publ);
738743

739744
map.addInteraction(modify);
@@ -757,7 +762,7 @@ var App = (function ($, publ) {
757762

758763
draw.on('drawend', function(evt) {
759764
(vector.getSource()).clear();
760-
publ.updateForm([evt.feature]);
765+
publ.updateForm([evt.feature], true);
761766
});
762767

763768
var control = new ol.control.Toggle({
@@ -847,13 +852,45 @@ var App = (function ($, publ) {
847852
/**
848853
*
849854
*/
850-
publ.updateForm = function (features) {
855+
publ.updateForm = function (features, updateAddressFlag) {
851856
var writer = new ol.format.GeoJSON();
852857
var geojson = JSON.parse(writer.writeFeatures(features, {
853858
featureProjection: 'EPSG:3857',
854859
dataProjection: 'EPSG:4326'
855860
}));
856861
$("#geom").val(JSON.stringify(geojson.features[0]));
862+
863+
if (updateAddressFlag && features && features.length > 0) {
864+
var addressInput = $("#issue-form #attributes label:contains('" + defaults.geocoderAddress + "')").parent("p").children("input");
865+
if (addressInput.length > 0) {
866+
// Todo: only works with point geometries for now for the last geometry
867+
var coords = features[features.length - 1].getGeometry().getCoordinates();
868+
coords = ol.proj.transform(coords,'EPSG:3857','EPSG:4326')
869+
$.getJSON("https://***REMOVED***/reversegeocode/json/" + coords.join(",") + ",1000", function(data) {
870+
var districtInput = $("#issue-form #attributes label:contains('" + defaults.geocoderDistrict + "')").parent("p").children("input");
871+
var foundDistrict = false;
872+
if (data.result.code >= 0 && data.result.address) {
873+
addressInput.val(data.result.address);
874+
if (districtInput.length > 0 && data.result.shikuchoson) {
875+
var regexp = /^(?:\S+)?(\S+)$/g;
876+
var match = regexp.exec(data.result.shikuchoson);
877+
if (match && match.length === 2) {
878+
districtInput.val(match[1]);
879+
foundDistrict = true;
880+
}
881+
}
882+
}
883+
else {
884+
addressInput.val("---");
885+
}
886+
if (!foundDistrict) {
887+
if (districtInput.length > 0) {
888+
districtInput.val("");
889+
}
890+
}
891+
});
892+
}
893+
}
857894
};
858895

859896
publ.getScale = function () {

0 commit comments

Comments
 (0)