我试图通过从数据库中获取半径来更改我的谷歌地图。现在我只得到“初始状态”半径,之后很难在地图上整体更新它。这与更新用户的位置也是同样的问题(我认为)。我正在使用 Firebase、React 和 Javascript。
这是我的地图代码:
import React from 'react';
import GoogleMapReact from 'google-map-react';
import Marker from './MapMarker';
import {getUserInfo} from '../firebaseConfig'
class SimpleMap extends React.Component {
state = {
lat: "",
lng: "",
userPos: "",
radius: 1500
};
static defaultProps = {
center: {
lat: 59.8,
lng: 17.63889
},
zoom: 11,
};
componentDidMount = () => {
navigator.geolocation.getCurrentPosition(this.currentCoords)
};
currentCoords = (position) => {
const latitude = position.coords.latitude
const longitude = position.coords.longitude
this.setState({
userPos: {lat: latitude, lng: longitude},
})
};
getRadius = () => {
getUserInfo().then((result) => {
this.setState({
radius: result.radius
})
});
}
render() {
const setCenter = this.state.userPos;
const setUserPos = this.state.userPos;
const setUserRadius = this.state.radius;
const apiIsLoaded = (map, maps, setUserPos, setUserRadius) => {
new maps.Circle({
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.3,
map,
center: setUserPos,
radius: setUserRadius
});
};
return (
<div style={{ height: '100vh', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{ key: //key }}
defaultCenter={this.props.center}
center={setCenter}
defaultZoom={this.props.zoom}
yesIWantToUseGoogleMapApiInternals={true}
onGoogleApiLoaded={({ map, maps }) => apiIsLoaded(map, maps, setUserPos, setUserRadius)}
>
<Marker
lat={setUserPos.lat}
lng={setUserPos.lng}
color="blue"
/>
</GoogleMapReact>
</div>
);
}
}
export default SimpleMap;