Reactjs 当对象移动时,如何在谷歌地图中添加标记是一个问题
我正在用API构建React应用程序来跟踪一些对象,我不使用任何库来管理谷歌地图。当我设置一个静态LatLng时,它会显示它应该在哪里,但当它得到每秒更新的数据时,它不会显示。 你能帮我吗Reactjs 当对象移动时,如何在谷歌地图中添加标记是一个问题,reactjs,google-maps,google-maps-api-3,Reactjs,Google Maps,Google Maps Api 3,我正在用API构建React应用程序来跟踪一些对象,我不使用任何库来管理谷歌地图。当我设置一个静态LatLng时,它会显示它应该在哪里,但当它得到每秒更新的数据时,它不会显示。 你能帮我吗 class GoogleMap extends Component { shouldComponentUpdate() { return false; } componentWillReceiveProps( nextProps ) { this.m
class GoogleMap extends Component {
shouldComponentUpdate() {
return false;
}
componentWillReceiveProps( nextProps ) {
this.map.panTo({lat: nextProps.lat, lng: nextProps.lng})
}
componentDidMount() {
// this.myLatLng = {lat: -25.363, lng: 131.044};
this.position = {
center: {
lat: this.props.lat,
lng: this.props.lng
}
};
this.mapOptions = {
center: this.position,
zoom: 5,
disableDefaultUI: true
};
this.map = new google.maps.Map(this.refs.map, this.mapOptions);
this.marker = new google.maps.Marker({
position: this.myLatLng,
map: this.map
});
}
render() {
return (
<div id="map" ref="map"/>
);
}
}
export default GoogleMap;
类GoogleMap扩展组件{
shouldComponentUpdate(){
返回false;
}
组件将接收道具(下一步){
this.map.panTo({lat:nextProps.lat,lng:nextProps.lng})
}
componentDidMount(){
//this.mylatng={lat:-25.363,lng:131.044};
此位置={
中心:{
拉特:这个,道具,拉特,
液化天然气:这个。道具。液化天然气
}
};
this.mapOptions={
中锋:这个位置,
缩放:5,
disableDefaultUI:true
};
this.map=new google.maps.map(this.refs.map,this.mapOptions);
this.marker=new google.maps.marker({
位置:this.myLatLng,
地图:这是地图
});
}
render(){
返回(
);
}
}
导出默认谷歌地图;
您必须使用marker.setPosition更新标记的位置,它需要
LatLng作为参数,并将标记移动到那里
这里是一个参考谷歌地图Javascript API的标记。
1)要更新标记位置,您可以使用标记
类,而不是将地图中心更改为给定的纬度/lng
2) 初始化位置
时有输入错误,的中心
属性的预期格式如下:
this.position = {
lat: this.props.lat,
lng: this.props.lng
};
以下示例演示如何在React中移动标记: