1
1
import { useEffect } from 'react' ;
2
- import { Feature as OlFeature } from 'ol' ;
3
2
import { fromLonLat , toLonLat } from 'ol/proj' ;
4
- import Style from 'ol/style/Style' ;
5
- import Icon from 'ol/style/Icon' ;
6
- import Point from 'ol/geom/Point' ;
7
- import VectorLayer from 'ol/layer/Vector' ;
8
3
import { useMap } from './Map' ;
9
4
import { getLonLat } from './util' ;
10
- import VectorSource from 'ol/source/Vector' ;
11
-
12
- export function getMarkerImage ( color = 'red' , text = 'A' ) {
13
- return `data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E` +
14
- `%3Csvg%20width%3D%2240px%22%20height%3D%2240px%22%20viewBox%3D%220%200%2015%2015%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E` +
15
- `%3Cpath%20fill%3D%22${ color } %22%20d%3D%22M7.5%2C0C5.0676%2C0%2C2.2297%2C1.4865%2C2.2297%2C5.2703%26%23xA%3B%26%23x9%3BC2.2297%2C7.8378%2C6.2838%2C13.5135%2C7.5%2C15c1.0811-1.4865%2C5.2703-7.027%2C5.2703-9.7297C12.7703%2C1.4865%2C9.9324%2C0%2C7.5%2C0z%22%2F%3E` +
16
- `%3Ctext%20x%3D%2250%25%22%20y%3D%2250%25%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%20font-size%3D%226px%22%3E` +
17
- text +
18
- `%3C%2Ftext%3E` +
19
- `%3C%2Fsvg%3E` ;
20
- }
5
+ import { addMarker , removeMarker } from './marker-func' ;
21
6
22
7
function getNextChar ( char ) {
23
8
const ascii = char . charCodeAt ( 0 ) ;
@@ -29,43 +14,6 @@ function getNextChar(char) {
29
14
}
30
15
}
31
16
32
-
33
- function addMarker ( markerLayer , lonLat , color , char ) {
34
- const markerFeature = new OlFeature ( {
35
- geometry : new Point ( fromLonLat ( lonLat ) )
36
- } ) ;
37
- markerFeature . setStyle ( new Style ( {
38
- image : new Icon ( {
39
- scale : 1 ,
40
- anchor : [ 0.5 , 40 ] ,
41
- anchorXUnits : 'fraction' ,
42
- anchorYUnits : 'pixels' ,
43
- src : getMarkerImage ( color , char )
44
- } ) ,
45
- } ) ) ;
46
-
47
- markerLayer . getSource ( ) . addFeature ( markerFeature ) ;
48
- return markerFeature ;
49
- }
50
-
51
- function getMarkerLayer ( map ) {
52
- const markerLayer = map . getLayers ( ) . getArray ( )
53
- . find ( el => el . get ( 'key' ) === 'markerLayer' ) as VectorLayer ;
54
- if ( markerLayer ) {
55
- return markerLayer ;
56
- }
57
-
58
- // container of markers, this can be set when marker is added, so that I can remove it here.
59
- const newMarkerLayer = new VectorLayer ( {
60
- source : new VectorSource ( { features : [ ] } ) ,
61
- properties : { key : 'markerLayer' } ,
62
- zIndex : 1 ,
63
- } ) ;
64
-
65
- map . addLayer ( newMarkerLayer ) ;
66
- return newMarkerLayer ;
67
- }
68
-
69
17
export function Marker ( {
70
18
lonLat= undefined ,
71
19
address= undefined ,
@@ -80,16 +28,14 @@ export function Marker({
80
28
useEffect ( ( ) => {
81
29
if ( ! map ) return ;
82
30
83
- const markerLayer = getMarkerLayer ( map ) ;
84
-
85
31
if ( lonLat ) {
86
- addMarker ( markerLayer , lonLat , color , markerChar ) ;
32
+ addMarker ( map , lonLat , color , markerChar ) ;
87
33
map . getView ( ) . setCenter ( fromLonLat ( lonLat ) )
88
34
} else if ( address ) {
89
35
getLonLat ( address ) . then ( resp => {
90
36
if ( resp [ 0 ] ) {
91
37
const lonLat = [ resp [ 0 ] . lon , resp [ 0 ] . lat ] ;
92
- const feature = addMarker ( markerLayer , lonLat , color , markerChar ) ;
38
+ const feature = addMarker ( map , lonLat , color , markerChar ) ;
93
39
feature . set ( 'address' , resp [ 0 ] . display_name )
94
40
map . getView ( ) . setCenter ( fromLonLat ( lonLat ) )
95
41
}
@@ -99,11 +45,11 @@ export function Marker({
99
45
map . on ( 'singleclick' , function ( evt ) {
100
46
const feature = map . forEachFeatureAtPixel ( evt . pixel , feature => feature ) ;
101
47
if ( feature ) {
102
- removeOnClick && markerLayer ?. getSource ( ) . removeFeature ( feature ) ;
48
+ removeOnClick && removeMarker ( map , feature ) ;
103
49
} else {
104
50
const clickLonLat = toLonLat ( evt . coordinate ) ;
105
51
markerChar = getNextChar ( markerChar ) ;
106
- addOnClick && addMarker ( markerLayer , clickLonLat , color , markerChar )
52
+ addOnClick && addMarker ( map , clickLonLat , color , markerChar )
107
53
}
108
54
} ) ;
109
55
} , [ map ] ) ;
0 commit comments