@@ -616,38 +616,43 @@ var App = (function ($, publ) {
616
616
) ;
617
617
618
618
$ ( "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 ( "" ) ;
638
650
}
639
651
}
640
652
}
641
- else {
642
- addressInput . val ( "---" ) ;
643
- }
644
- if ( ! foundDistrict ) {
645
- districtInput . val ( "" ) ;
646
- }
647
- } ) ;
653
+ } )
648
654
}
649
655
} ) ;
650
-
651
656
}
652
657
653
658
if ( $ ( "#issue-form #attributes button.btn-parksearch" ) . length == 0 ) {
@@ -733,7 +738,7 @@ var App = (function ($, publ) {
733
738
} ) ;
734
739
735
740
modify . on ( 'modifyend' , function ( evt ) {
736
- this . updateForm ( evt . features . getArray ( ) ) ;
741
+ this . updateForm ( evt . features . getArray ( ) , true ) ;
737
742
} , publ ) ;
738
743
739
744
map . addInteraction ( modify ) ;
@@ -757,7 +762,7 @@ var App = (function ($, publ) {
757
762
758
763
draw . on ( 'drawend' , function ( evt ) {
759
764
( vector . getSource ( ) ) . clear ( ) ;
760
- publ . updateForm ( [ evt . feature ] ) ;
765
+ publ . updateForm ( [ evt . feature ] , true ) ;
761
766
} ) ;
762
767
763
768
var control = new ol . control . Toggle ( {
@@ -847,13 +852,45 @@ var App = (function ($, publ) {
847
852
/**
848
853
*
849
854
*/
850
- publ . updateForm = function ( features ) {
855
+ publ . updateForm = function ( features , updateAddressFlag ) {
851
856
var writer = new ol . format . GeoJSON ( ) ;
852
857
var geojson = JSON . parse ( writer . writeFeatures ( features , {
853
858
featureProjection : 'EPSG:3857' ,
854
859
dataProjection : 'EPSG:4326'
855
860
} ) ) ;
856
861
$ ( "#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
+ }
857
894
} ;
858
895
859
896
publ . getScale = function ( ) {
0 commit comments