我正在尝试在传单地图上显示两点之间的路线。为此,我正在使用传单路由机。但是我将地图对象传递给传单路由组件时遇到问题。
map_container.js
...
return (
<Map ref='map' center={position} zoom={this.state.zoom}>
<TileLayer
attribution="&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors"
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Routing map={this.refs.map} from={[51.599684, 15.27539]} to={[51.602292, 15.295128]} />
</Map>
)
...
路由.js
import {MapLayer} from 'react-leaflet';
import L from 'leaflet';
import 'leaflet-routing-machine';
export default class RoutingMachine extends MapLayer {
componentWillMount() {
super.componentWillMount();
this.leafletElement.addTo(this.props.map);
}
render() {
return null;
}
createLeafletElement (props) {
const {from, to} = this.props;
console.log(this.props)
var leafletElement = L.Routing.control({
waypoints: [
L.latLng(from[0], from[1]),
L.latLng(to[0], to[1]),
],
});
return leafletElement;
}
}
我得到的错误:
{map: undefined, from: Array(2), to: Array(2)}
bundle.js:69506 Uncaught TypeError: Cannot read property 'getSize' of undefined
at NewClass.onAdd (bundle.js:69506)
at NewClass.onAdd (bundle.js:68679)
at NewClass.addTo (bundle.js:26718)
但最有趣的是 -在 "react-leaflet": "1.1.0" 版本上一切都可以完美运行,但在 1.1.1 及更高版本上它会中断。
有任何想法吗?