@@ -605,57 +605,43 @@ var App = (function ($, publ) {
605
605
) ;
606
606
607
607
$ ( "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 ( "" ) ;
627
639
}
628
640
}
629
641
}
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
+ } )
656
643
}
657
644
} ) ;
658
-
659
645
}
660
646
661
647
if ( $ ( "#issue-form #attributes button.btn-parksearch" ) . length == 0 ) {
@@ -741,7 +727,7 @@ var App = (function ($, publ) {
741
727
} ) ;
742
728
743
729
modify . on ( 'modifyend' , function ( evt ) {
744
- this . updateForm ( evt . features . getArray ( ) ) ;
730
+ this . updateForm ( evt . features . getArray ( ) , true ) ;
745
731
} , publ ) ;
746
732
747
733
map . addInteraction ( modify ) ;
@@ -765,7 +751,7 @@ var App = (function ($, publ) {
765
751
766
752
draw . on ( 'drawend' , function ( evt ) {
767
753
( vector . getSource ( ) ) . clear ( ) ;
768
- publ . updateForm ( [ evt . feature ] ) ;
754
+ publ . updateForm ( [ evt . feature ] , true ) ;
769
755
} ) ;
770
756
771
757
var control = new ol . control . Toggle ( {
@@ -855,13 +841,45 @@ var App = (function ($, publ) {
855
841
/**
856
842
*
857
843
*/
858
- publ . updateForm = function ( features ) {
844
+ publ . updateForm = function ( features , updateAddressFlag ) {
859
845
var writer = new ol . format . GeoJSON ( ) ;
860
846
var geojson = JSON . parse ( writer . writeFeatures ( features , {
861
847
featureProjection : 'EPSG:3857' ,
862
848
dataProjection : 'EPSG:4326'
863
849
} ) ) ;
864
850
$ ( "#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
+ }
865
883
} ;
866
884
867
885
publ . getScale = function ( ) {
0 commit comments