我想在项目中为当前位置添加一个蓝点。我的地图文件的代码是这种方式
状态是
this.state={
currentPosition:{lat: 26.84 ,lng: 75.80},
destinationPosition:{lat: 26.84 ,lng: 75.80}
};
当前位置的handleClick是
handleClick(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition((position)=>{
this.setState(()=>({
currentPosition:{
lat:position.coords.latitude,
lng:position.coords.longitude}}))
console.log(position.coords.latitude);
});
}
else{
alert("Geoloaction is not supported by your browser");
}
}
渲染功能就是这样
render(){
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyC5VMMlyr_A6K5ycpOrq3OsVM8YYbn0q3A&v=3.exp&libraries=geometry,drawing,places",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `300px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withScriptjs,
withGoogleMap
)((props) =>
<GoogleMap defaultZoom={15} defaultCenter=
{this.state.destinationPosition} >
<Marker position={this.state.destinationPosition} draggable
changeLat
onDragEnd={this.onMarkerPositionChanged.bind(this)}
/>
<Button bsStyle="success" onClick=
{this.handleClick.bind(this)}>Current Position</Button>
</GoogleMap>
);
return(
<Container
text={<Text lat={this.state.destinationPosition.lat}
lng={this.state.destinationPosition.lng}/>}
map={<MyMapComponent />}
/>
);
}
另外,请告诉我如何在地图中同时包含标记和蓝点。
最佳答案
render() {
const MyMapComponent = compose(
withProps({
googleMapURL:
'https://maps.googleapis.com/maps/api/js?key=AIzaSyC5VMMlyr_A6K5ycpOrq3OsVM8YYbn0q3A&v=3.exp&libraries=geometry,drawing,places',
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `300px` }} />,
mapElement: <div style={{ height: `100%` }} />
}),
withScriptjs,
withGoogleMap
)(props => (
<GoogleMap defaultZoom={15} defaultCenter={this.state.destinationPosition}>
<Marker
position={this.state.destinationPosition}
draggable
changeLat
onDragEnd={this.onMarkerPositionChanged.bind(this)}
/>
//this is your current location blue dot
//could replace it with the icon you like
<Marker
icon="https://www.robotwoods.com/dev/misc/bluecircle.png"
position={this.state.currentPosition}
/>
<Button bsStyle="success" onClick={this.handleClick.bind(this)}>
Current Position
</Button>
</GoogleMap>
));
return (
<Container
text={
<Text
lat={this.state.destinationPosition.lat}
lng={this.state.destinationPosition.lng}
/>
}
map={<MyMapComponent />}
/>
);
}