Skip to content

Commit 18e0eba

Browse files
committed
Fixed geocoding button in map panel
Signed-off-by: Ko Nagase <[email protected]>
1 parent 0653838 commit 18e0eba

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

src/components/gtt-client.ts

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Mask from 'ol-ext/filter/Mask'
2929
import Bar from 'ol-ext/control/Bar'
3030
import Toggle from 'ol-ext/control/Toggle'
3131
import Button from 'ol-ext/control/Button'
32+
import TextButton from 'ol-ext/control/TextButton'
3233
import LayerPopup from 'ol-ext/control/LayerPopup'
3334
import Popup from 'ol-ext/overlay/Popup'
3435

@@ -241,7 +242,7 @@ export class GttClient {
241242
this.map.addControl(this.toolbar)
242243
this.setView()
243244
this.setGeolocation()
244-
this.setGeocoding()
245+
this.setGeocoding(this.map)
245246
this.parseHistory()
246247

247248
// Control button
@@ -867,7 +868,7 @@ export class GttClient {
867868
/**
868869
* Add Geocoding functionality
869870
*/
870-
setGeocoding():void {
871+
setGeocoding(currentMap: Map):void {
871872

872873
// Hack to add Geocoding buttons to text fields
873874
// There should be a better way to do this
@@ -1034,23 +1035,30 @@ export class GttClient {
10341035
}
10351036

10361037
// disable geocoder control if geocoderUrl is null
1037-
if (!this.defaults.geocoderUrl) {
1038+
if (!geocoder.geocode_url) {
10381039
return
10391040
}
10401041

1042+
const mapId = currentMap.getTargetElement().getAttribute("id")
1043+
10411044
// Control button
10421045
const geocodingCtrl = new Toggle({
1043-
html: '<i class="gtt-icon-info" ></i>',
1046+
html: '<i class="gtt-icon-search" ></i>',
10441047
title: "Geocoding",
10451048
className: "ctl-geocoding",
10461049
onToggle: (active: boolean) => {
1050+
const text = (document.querySelector("div#" + mapId + " .ctl-geocoding div input") as HTMLInputElement)
10471051
if (active) {
1048-
(document.querySelector(".ctl-geocoding button input") as HTMLInputElement).focus()
1052+
text.focus()
1053+
} else {
1054+
text.blur()
1055+
const button = document.querySelector<HTMLButtonElement>("div#" + mapId + " .ctl-geocoding button")
1056+
button.blur()
10491057
}
10501058
},
10511059
bar: new Bar({
10521060
controls: [
1053-
new Button({
1061+
new TextButton({
10541062
html: '<form><input name="address" type="text" /></form>'
10551063
} as any)
10561064
]
@@ -1059,29 +1067,42 @@ export class GttClient {
10591067
this.toolbar.addControl(geocodingCtrl)
10601068

10611069
// Make Geocoding API request
1062-
document.querySelector(".ctl-geocoding form").addEventListener('submit', (evt) => {
1063-
evt.preventDefault()
1070+
document.querySelector<HTMLInputElement>("div#" + mapId + " .ctl-geocoding div input").addEventListener('keydown', (evt) => {
1071+
if (evt.keyCode === 13) {
1072+
evt.preventDefault()
1073+
evt.stopPropagation()
1074+
} else {
1075+
return true
1076+
}
10641077

1065-
if (!this.defaults.geocoderUrl) {
1078+
if (!geocoder.geocode_url) {
10661079
throw new Error ("No Geocoding service configured!")
10671080
}
10681081

1069-
const url = [
1070-
this.defaults.geocoderUrl,
1071-
encodeURIComponent((document.querySelector('.ctl-geocoding form input[name=address]') as HTMLInputElement).value)
1072-
]
1082+
const url = geocoder.geocode_url.replace("{address}", encodeURIComponent(
1083+
(document.querySelector("div#" + mapId + " .ctl-geocoding form input[name=address]") as HTMLInputElement).value)
1084+
)
10731085

1074-
fetch(url.join('/'))
1086+
fetch(url)
10751087
.then(response => response.json())
10761088
.then(data => {
1077-
// TODO, check for valid results
1078-
const address = new GeoJSON().readFeature(data, {
1079-
featureProjection: 'EPSG:3857'
1080-
})
1081-
this.map.getView().fit(address.getGeometry().getExtent(), {
1082-
size: this.map.getSize()
1083-
})
1089+
const check = evaluateComparison(getObjectPathValue(data, geocoder.geocode_result_check_path),
1090+
geocoder.geocode_result_check_operator,
1091+
geocoder.geocode_result_check_value
1092+
)
1093+
if (check) {
1094+
const lon = getObjectPathValue(data, geocoder.geocode_result_lon_path)
1095+
const lat = getObjectPathValue(data, geocoder.geocode_result_lat_path)
1096+
const coords = [lon, lat]
1097+
const geom = new Point(fromLonLat(coords, 'EPSG:3857'))
1098+
currentMap.getView().fit(geom.getExtent(), {
1099+
size: currentMap.getSize(),
1100+
maxZoom: parseInt(this.defaults.fitMaxzoom)
1101+
})
1102+
}
10841103
})
1104+
1105+
return false
10851106
})
10861107
}
10871108

0 commit comments

Comments
 (0)