【问题标题】:blue dot for current location in react-google-mapsreact-google-maps 中当前位置的蓝点
【发布时间】:2019-03-28 17:48:27
【问题描述】:

我想在我的项目中为我当前的位置添加蓝点。我的地图文件的代码是这样的

状态是

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函数是这样的

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 />} 
      />

    );
}

另外请告诉我如何在我的地图中同时包含标记和蓝点。

【问题讨论】:

    标签: angularjs node.js reactjs react-google-maps


    【解决方案1】:
    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 />}
            />
        );
    }
    

    【讨论】:

    • 你能告诉我如何停止在地图上重新渲染,因为当我更改状态时,地图会再次加载。除了状态之外,他们还有什么更好的方法来存储位置。
    • 您可以将您的位置存储在状态中,这完全可以。不确定“地图再次加载”是什么意思。如果您的意思是标记的位置发生了变化,那很好。如果你的意思是整个地图都被刷新了,它可能与你如何构建你的 有关。您可能应该直接渲染您的“文本”和“地图”,而不是将它们传递给 这是我的猜测,因为我没有您的所有代码。
    • 我明白你在说什么,你想让我在 中导入 ,但是我将如何使用我作为道具传递的状态.
    • 在你的 中,从“props”而不是“this.state”中读取数据。您可以将“this.state”作为道具传递给 ,然后在 中将此道具传递给
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多