我想在Map上添加模块“ React-leaflet-locate-control”。不幸的是,我遇到此错误“ TypeError:无法读取未定义的属性'addLayer'”,而且我不知道如何纠正此错误。
能帮我吗?
这是我的组件Map:
import './Map.css';
import React, { Component } from 'react';
import { Map, TileLayer, Marker, Popup } from 'react-leaflet';
import L from "leaflet";
import { getLat, getLng } from '../../Store.js';
import SearchBar from '../SearchBar/SearchBar.js';
import LocateControl from 'react-leaflet-locate-control';
const customMarker = new L.icon({
iconUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-icon.png",
iconSize: [25, 41],
iconAnchor: [13, 0]
});
export default class MapLeaflet extends Component {
constructor(props) {
super(props);
this.state = {
lat: getLat(),
lng: getLng(),
}
}
updateMarker = (e) => {
this.props.updateMarkerPosition(e.latlng.lat, e.latlng.lng);
this.setState({
lat: e.latlng.lat,
lng: e.latlng.lng
})
}
render() {
const position = [this.state.lat, this.state.lng]
const locateOptions = {
position: 'topright',
strings: {
title: 'Show me where I am, yo!'
},
onActivate: () => {} // callback before engine starts retrieving locations
}
return (
<div className="map">
<Map center={position} zoom={13} className="map" onClick={this.updateMarker}>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={position} icon={customMarker}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
<SearchBar />
<LocateControl options={locateOptions} startDirectly/>
</Map>
</div>
)
}
}
答案 0 :(得分:3)
react-leaflet-locate-control
package与react-leaflet
的最新版本(v2)不兼容,实际上已经报道了类似的问题here
由于react-leaflet-locate-control
表示leaflet-locatecontrol
plugin的包装,因此可以使用以下react-leaflet
的自定义组件来提供与react-leaflet-locate-control
相同的功能:
import React, { Component } from "react";
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);
安装
1)安装插件:
npm install leaflet.locatecontrol
2)在
public/index.html
中包含以下CSS资源:<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet.locatecontrol/dist/L.Control.Locate.min.css">
这里是a demo(source code)
答案 1 :(得分:0)
如果您像我一样使用Typescript。我使用下面的组件代码使其与v2
一起使用
import L from 'leaflet';
import { withLeaflet, MapControl, MapControlProps } from 'react-leaflet';
import Locate from 'leaflet.locatecontrol';
import './Geolocate.css';
type GeolocateProps = L.Control.LocateOptions & MapControlProps;
class Geolocate extends MapControl<GeolocateProps> {
createLeafletElement(props: GeolocateProps) {
const { leaflet, ...options } = props;
const locate = Locate as any;
const lc = new locate(options);
return lc;
}
}
export default withLeaflet(Geolocate);
此外,您可以跳过真棒字体和库自身的CSS文件,而提供自己的样式
Geolocate.css
.leaflet-control-locate a.leaflet-bar-part div {
background-image: url(../../images/geolocation.png);
background-size: 22px 74px;
background-position: top 2px left 2px;
background-repeat: no-repeat;
width: 30px;
height: 30px;
}
.leaflet-control-locate a.leaflet-bar-part div.loading {
background-position: top -24px left 2px;
}
.leaflet-control-locate.active a.leaflet-bar-part div.locate {
background-position: top -50px left 2px;
}
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
.leaflet-control-locate a.leaflet-bar-part div {
background-image: url(../../images/geolocation-2x.png);
}
}
当然,您必须先使用leaflet.locatecontrol
安装npm
npm i leaflet.locatecontrol
希望这会有所帮助。
答案 2 :(得分:0)
实际上,您不需要任何外部模块即可在react或任何其他JavaScript库中获取当前位置
您可以使用
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => //Do something with it );
}
这是一种反应方式:
const [currentLocation, setCurrentLocation] = React.useState([0, 0]);
//change [0,0] to something that makes sense to your users in case they deny access to location.
useEffect(() => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position =>
setCurrentLocation([position.coords.latitude, position.coords.longitude]))
}
}, []);
return (
<Map center={currentLocation} zoom={15}>
// the rest ie . TileLayer,Marker,Popup
</Map>