Javascript _onLocationFound没有';位置控制
我正在使用传单.locatecontrol获取用户在地图中的位置,但是_onLocationFound不起作用。 这是我的地图组件Javascript _onLocationFound没有';位置控制,javascript,reactjs,leaflet,react-leaflet,Javascript,Reactjs,Leaflet,React Leaflet,我正在使用传单.locatecontrol获取用户在地图中的位置,但是_onLocationFound不起作用。 这是我的地图组件 <Map ref={ref} center={initialState ? initialState.center : DEFAULT_CENTER} zoom={initialState ? initialState.zoom : 5} maxZoom={18} on
<Map
ref={ref}
center={initialState ? initialState.center : DEFAULT_CENTER}
zoom={initialState ? initialState.zoom : 5}
maxZoom={18}
onMove={(e) => {
onMapMove(e);
}}
>
<TileLayer
url={`${settings.getConfig().MAP_TILE_URL}${
settings.getConfig().MAP_X_API_KEY
}`}
attribution={settings.getConfig().MAP_ATTRIBUTION}
/>
{marker && (
<Marker
position={marker}
icon={L.icon({
iconUrl: "https://webstockreview.net/images/map-icon-png-6.png",
iconSize: [25, 30], // size of the icon
iconAnchor: [13, 36], // point of the icon which will correspond to marker's location
})}
/>
)}
<LocateControl options={locateOptions} />
</Map>
LocateControl是我实现的用于分离逻辑的简单组件,下面是它的代码:
import { withLeaflet } from "react-leaflet";
import Locate from "leaflet.locatecontrol";
class LocateControl extends Component {
componentDidMount() {
const { options, startDirectly } = this.props;
const { map } = this.props.leaflet;
const lc = new Locate(options);
lc.addTo(map);
if (startDirectly) {
// request location update and set location
lc.start();
}
}
render() {
return null;
}
}
export default withLeaflet(LocateControl);
我的问题是,我想在找到位置时保存它,但是_onLocationFound函数不会被触发,即使位置在地图中被更新,我也无法找出我遗漏了什么,因为即使在文档中,该函数也只是过了locateControl
如果有人能给我一个提示,我将不胜感激。您正在传递一个名为\u onLocationFound
的选项,该选项在
您这样做可能是因为您查看了传单.LocateControl的代码,并将onLocationError
选项与\u onLocationError
方法混淆,而没有注意到\u onLocationError
方法显式调用其同名选项
…但是您没有意识到根本没有onLocationFound
选项,并且内部\u onLocationFound
方法根本不调用任何用户提供的回调
相反,这样做:利用地图上的locationfound
事件
import { withLeaflet } from "react-leaflet";
import Locate from "leaflet.locatecontrol";
class LocateControl extends Component {
componentDidMount() {
const { options, startDirectly } = this.props;
const { map } = this.props.leaflet;
const lc = new Locate(options);
lc.addTo(map);
if (startDirectly) {
// request location update and set location
lc.start();
}
}
render() {
return null;
}
}
export default withLeaflet(LocateControl);
_onLocationError: function(err) {
/* snip */
this.options.onLocationError(err, this);
},