【问题标题】:Issues with Google Maps Javascript API, Marker and Polylines NOT SHOWING together on a React.js AppGoogle Maps Javascript API、标记和折线在 React.js 应用程序上未同时显示的问题
【发布时间】:2020-10-27 05:13:13
【问题描述】:

我很确定 states 和 setState 的东西是正确的,所以这是我的代码。我在这里所做的是为地图键分配了函数this.initMap()。要详细查看:

class Radar extends Component{
    constructor(props){
        super(props)
        //initialize methods here
        this.googleMap = React.createRef()

        //define states here
        this.state = {
            cityname:this.props.datasource.name,
            cityCoordinates:[this.props.datasource.coord.lon, this.props.datasource.coord.lat],
            winddirection:this.props.datasource.wind.deg,
            cityPool:[],
            borderLine:[]
        }
}


initMap(){
    return new window.google.maps.Map(this.googleMap.current,{
        zoom: 7.5,
        center:{ lat: this.state.cityCoordinates[1], lng:this.state.cityCoordinates[0] },
      disableDefaultUI: true,
    })
}

targetedCityMarker(){
    new window.google.maps.Marker({
        position: { lat: this.state.cityCoordinates[1], lng:this.state.cityCoordinates[0] },
        map:this.initMap()
    })
}

cityPoolPolyLine(){
    let citypool_coordinates=[]
    this.state.cityPool.map((i)=>{
        citypool_coordinates.push(i.location)
        return citypool_coordinates
    })

    console.log(this.state.borderLine)   
    console.log(citypool_coordinates)    
    new window.google.maps.Polyline({
        path: this.state.borderLine,
        geodesic: true,
        strokeColor: "#2AA181",
        strokeOpacity: 1.0,
        strokeWeight: 2,
        map:this.initMap()
      });
}

componentDidMount(){
    axios.get('http://localhost:5000/weather/radar').then((res)=>{
        console.log(res.data)
        this.setState({
            cityPool:res.data
        })
    })

    axios.get('http://localhost:5000/weather/radar_2').then((res)=>{
        this.setState({
            borderLine:res.data
        })
    })

    const MapCode = document.createElement('script')
    MapCode.src =`https://maps.googleapis.com/maps/api/js?key=${process.env.REACT_APP_GOOGLE_MAPS_API_KEY}&libraries=&v=weekly`
    window.document.body.appendChild(MapCode)
    

    MapCode.addEventListener('load', ()=>{
        this.initMap()
        this.targetedCityMarker()
        setTimeout(()=>{
            this.cityPoolPolyLine()
        },4000)
    })
}
    
    render(){
        return(
            <div className='radar-page'>
                <h1>Weather Radar</h1>
                <p>City Name: {this.state.cityname}</p>
                <p>City Coordinates:Lon: {this.state.cityCoordinates[0]}, Lat: {this.state.cityCoordinates[1]}</p>
                <p>Wind Direction: {this.state.winddirection}˚ </p>
                <p>Selected Cities: * Write a query, setup the range, greater or less than the chosen city's coordinates *</p>
                <div id="google-map" ref={this.googleMap} style={{ width: '500px', height: '400px' }} />
            </div>
        )
    }
}

export default Radar

我对@9​​87654323@ 的使用正确吗?我的直觉告诉我这条线或我对 OOP 的一些困惑正在造成一些麻烦。所以一旦我运行,它首先显示没有折线的标记,然后标记消失但折线出现。

【问题讨论】:

    标签: javascript reactjs google-maps oop


    【解决方案1】:

    发生这种情况是因为您多次实例化地图。每次调用this.initMap() 时,您都会创建一个新的地图实例,因此您应该只调用此方法一次(可能在构造函数中),将其分配给某个变量,然后在Marker() 中引用您的地图时使用该变量和Polyline() 方法。

    【讨论】:

    • 看到了吗?我知道你能帮助我,你做到了!
    • 但只是一个后续问题,所以我的问题是我多次实例化我的地图。标记功能使用的是旧地图实例,而折线使用的是最新的实例,这就是为什么没有显示标记?对吗?
    猜你喜欢
    • 1970-01-01
    • 2022-11-26
    • 2014-07-29
    • 1970-01-01
    • 2015-07-01
    • 2014-10-10
    • 1970-01-01
    • 2012-04-12
    • 2014-02-14
    相关资源
    最近更新 更多