1
1
import * as React from 'react' ;
2
2
import { useState } from 'react' ;
3
- import { useControl , Marker , ControlPosition } from 'react-map-gl' ;
4
- import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder' ;
3
+ import { useControl , Marker , MarkerProps , ControlPosition } from 'react-map-gl' ;
4
+ import MapboxGeocoder , { GeocoderOptions } from '@mapbox/mapbox-gl-geocoder' ;
5
5
6
- type GeocoderControlProps = {
6
+ type GeocoderControlProps = Omit < GeocoderOptions , 'accessToken' | 'mapboxgl' | 'marker' > & {
7
7
mapboxAccessToken : string ;
8
- origin ?: string ;
9
- zoom ?: number ;
10
- flyTo ?: boolean | object ;
11
- placeholder ?: string ;
12
- proximity ?: {
13
- longitude : number ;
14
- latitude : number ;
15
- } ;
16
- trackProximity ?: boolean ;
17
- collapsed ?: boolean ;
18
- clearAndBlurOnEsc ?: boolean ;
19
- clearOnBlur ?: boolean ;
20
- box ?: [ number , number , number , number ] ;
21
- countries ?: string ;
22
- types ?: string ;
23
- minLength ?: number ;
24
- limit ?: number ;
25
- language ?: string ;
26
- filter ?: ( feature : object ) => boolean ;
27
- localGeocoder ?: Function ;
28
- externalGeocoder ?: Function ;
29
- reverseMode ?: 'distance' | 'score' ;
30
- reverseGeocode ?: boolean ;
31
- enableEventLogging ?: boolean ;
32
- marker ?: boolean | object ;
33
- render ?: ( feature : object ) => string ;
34
- getItemValue ?: ( feature : object ) => string ;
35
- mode ?: 'mapbox.places' | 'mapbox.places-permanent' ;
36
- localGeocoderOnly ?: boolean ;
37
- autocomplete ?: boolean ;
38
- fuzzyMatch ?: boolean ;
39
- routing ?: boolean ;
40
- worldview ?: string ;
8
+ marker ?: boolean | Omit < MarkerProps , 'longitude' | 'latitude' > ;
41
9
42
10
position : ControlPosition ;
43
11
@@ -55,6 +23,7 @@ export default function GeocoderControl(props: GeocoderControlProps) {
55
23
( ) => {
56
24
const ctrl = new MapboxGeocoder ( {
57
25
...props ,
26
+ marker : false ,
58
27
accessToken : props . mapboxAccessToken
59
28
} ) ;
60
29
ctrl . on ( 'loading' , props . onLoading ) ;
@@ -66,7 +35,7 @@ export default function GeocoderControl(props: GeocoderControlProps) {
66
35
const location =
67
36
result &&
68
37
( result . center || ( result . geometry ?. type === 'Point' && result . geometry . coordinates ) ) ;
69
- if ( location ) {
38
+ if ( location && props . marker ) {
70
39
setMarker ( < Marker { ...props . marker } longitude = { location [ 0 ] } latitude = { location [ 1 ] } /> ) ;
71
40
} else {
72
41
setMarker ( null ) ;
@@ -80,6 +49,7 @@ export default function GeocoderControl(props: GeocoderControlProps) {
80
49
}
81
50
) ;
82
51
52
+ // @ts -ignore (TS2339) private member
83
53
if ( geocoder . _map ) {
84
54
if ( geocoder . getProximity ( ) !== props . proximity && props . proximity !== undefined ) {
85
55
geocoder . setProximity ( props . proximity ) ;
@@ -94,48 +64,50 @@ export default function GeocoderControl(props: GeocoderControlProps) {
94
64
geocoder . setZoom ( props . zoom ) ;
95
65
}
96
66
if ( geocoder . getFlyTo ( ) !== props . flyTo && props . flyTo !== undefined ) {
97
- geocoder . setFlyTo ( props . zoom ) ;
67
+ geocoder . setFlyTo ( props . flyTo ) ;
98
68
}
99
69
if ( geocoder . getPlaceholder ( ) !== props . placeholder && props . placeholder !== undefined ) {
100
- geocoder . setPlaceholder ( props . zoom ) ;
70
+ geocoder . setPlaceholder ( props . placeholder ) ;
101
71
}
102
72
if ( geocoder . getCountries ( ) !== props . countries && props . countries !== undefined ) {
103
- geocoder . setCountries ( props . zoom ) ;
73
+ geocoder . setCountries ( props . countries ) ;
104
74
}
105
75
if ( geocoder . getTypes ( ) !== props . types && props . types !== undefined ) {
106
- geocoder . setTypes ( props . zoom ) ;
76
+ geocoder . setTypes ( props . types ) ;
107
77
}
108
78
if ( geocoder . getMinLength ( ) !== props . minLength && props . minLength !== undefined ) {
109
- geocoder . setMinLength ( props . zoom ) ;
79
+ geocoder . setMinLength ( props . minLength ) ;
110
80
}
111
81
if ( geocoder . getLimit ( ) !== props . limit && props . limit !== undefined ) {
112
- geocoder . setLimit ( props . zoom ) ;
82
+ geocoder . setLimit ( props . limit ) ;
113
83
}
114
84
if ( geocoder . getFilter ( ) !== props . filter && props . filter !== undefined ) {
115
- geocoder . setFilter ( props . zoom ) ;
85
+ geocoder . setFilter ( props . filter ) ;
116
86
}
117
87
if ( geocoder . getOrigin ( ) !== props . origin && props . origin !== undefined ) {
118
- geocoder . setOrigin ( props . zoom ) ;
119
- }
120
- if ( geocoder . getAutocomplete ( ) !== props . autocomplete && props . autocomplete !== undefined ) {
121
- geocoder . setAutocomplete ( props . zoom ) ;
122
- }
123
- if ( geocoder . getFuzzyMatch ( ) !== props . fuzzyMatch && props . fuzzyMatch !== undefined ) {
124
- geocoder . setFuzzyMatch ( props . zoom ) ;
125
- }
126
- if ( geocoder . getRouting ( ) !== props . routing && props . routing !== undefined ) {
127
- geocoder . setRouting ( props . zoom ) ;
128
- }
129
- if ( geocoder . getWorldview ( ) !== props . worldview && props . worldview !== undefined ) {
130
- geocoder . setWorldview ( props . zoom ) ;
131
- }
88
+ geocoder . setOrigin ( props . origin ) ;
89
+ }
90
+ // Types missing from @types /mapbox__mapbox-gl-geocoder
91
+ // if (geocoder.getAutocomplete() !== props.autocomplete && props.autocomplete !== undefined) {
92
+ // geocoder.setAutocomplete(props.autocomplete);
93
+ // }
94
+ // if (geocoder.getFuzzyMatch() !== props.fuzzyMatch && props.fuzzyMatch !== undefined) {
95
+ // geocoder.setFuzzyMatch(props.fuzzyMatch);
96
+ // }
97
+ // if (geocoder.getRouting() !== props.routing && props.routing !== undefined) {
98
+ // geocoder.setRouting(props.routing);
99
+ // }
100
+ // if (geocoder.getWorldview() !== props.worldview && props.worldview !== undefined) {
101
+ // geocoder.setWorldview(props.worldview);
102
+ // }
132
103
}
133
104
return marker ;
134
105
}
135
106
136
107
const noop = ( ) => { } ;
137
108
138
109
GeocoderControl . defaultProps = {
110
+ marker : true ,
139
111
onLoading : noop ,
140
112
onResults : noop ,
141
113
onResult : noop ,
0 commit comments