@@ -29,6 +29,7 @@ import Mask from 'ol-ext/filter/Mask'
29
29
import Bar from 'ol-ext/control/Bar'
30
30
import Toggle from 'ol-ext/control/Toggle'
31
31
import Button from 'ol-ext/control/Button'
32
+ import TextButton from 'ol-ext/control/TextButton'
32
33
import LayerPopup from 'ol-ext/control/LayerPopup'
33
34
import Popup from 'ol-ext/overlay/Popup'
34
35
@@ -241,7 +242,7 @@ export class GttClient {
241
242
this . map . addControl ( this . toolbar )
242
243
this . setView ( )
243
244
this . setGeolocation ( )
244
- this . setGeocoding ( )
245
+ this . setGeocoding ( this . map )
245
246
this . parseHistory ( )
246
247
247
248
// Control button
@@ -867,7 +868,7 @@ export class GttClient {
867
868
/**
868
869
* Add Geocoding functionality
869
870
*/
870
- setGeocoding ( ) :void {
871
+ setGeocoding ( currentMap : Map ) :void {
871
872
872
873
// Hack to add Geocoding buttons to text fields
873
874
// There should be a better way to do this
@@ -1034,23 +1035,30 @@ export class GttClient {
1034
1035
}
1035
1036
1036
1037
// disable geocoder control if geocoderUrl is null
1037
- if ( ! this . defaults . geocoderUrl ) {
1038
+ if ( ! geocoder . geocode_url ) {
1038
1039
return
1039
1040
}
1040
1041
1042
+ const mapId = currentMap . getTargetElement ( ) . getAttribute ( "id" )
1043
+
1041
1044
// Control button
1042
1045
const geocodingCtrl = new Toggle ( {
1043
- html : '<i class="gtt-icon-info " ></i>' ,
1046
+ html : '<i class="gtt-icon-search " ></i>' ,
1044
1047
title : "Geocoding" ,
1045
1048
className : "ctl-geocoding" ,
1046
1049
onToggle : ( active : boolean ) => {
1050
+ const text = ( document . querySelector ( "div#" + mapId + " .ctl-geocoding div input" ) as HTMLInputElement )
1047
1051
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 ( )
1049
1057
}
1050
1058
} ,
1051
1059
bar : new Bar ( {
1052
1060
controls : [
1053
- new Button ( {
1061
+ new TextButton ( {
1054
1062
html : '<form><input name="address" type="text" /></form>'
1055
1063
} as any )
1056
1064
]
@@ -1059,29 +1067,42 @@ export class GttClient {
1059
1067
this . toolbar . addControl ( geocodingCtrl )
1060
1068
1061
1069
// 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
+ }
1064
1077
1065
- if ( ! this . defaults . geocoderUrl ) {
1078
+ if ( ! geocoder . geocode_url ) {
1066
1079
throw new Error ( "No Geocoding service configured!" )
1067
1080
}
1068
1081
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
+ )
1073
1085
1074
- fetch ( url . join ( '/' ) )
1086
+ fetch ( url )
1075
1087
. then ( response => response . json ( ) )
1076
1088
. 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
+ }
1084
1103
} )
1104
+
1105
+ return false
1085
1106
} )
1086
1107
}
1087
1108
0 commit comments