Issues with Google Maps Javascript API, Marker and Polylines NOT SHOWING together on a React.js App

LearnerSamuelX

I am pretty sure the states and setState stuff are correct, so here is my code. What I did here was I assigned the map key with the function this.initMap(). To have a detailed look:

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

Is my use of this.initMap() correct? My instinct tells me this line or some of my confusion in regard with OOP is causing some troubles. So once I run, it shows the marker first with no polyline, and then the marker disappears but the poly line shows up.

errata

This is happening because you instantiate your map multiple times. Every time you call this.initMap() you create a new map instance, so what you should do is call this method only once (probably in constructor), assign it to some variable and then use that variable when referring to your map in Marker() and Polyline() methods.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Plotting multiple polylines on Google Maps

分類Dev

Google Maps JavaScript API v3 changing marker and add satellite view

分類Dev

Blazor:Google Maps JS API

分類Dev

React js google map react-google-maps-api add StandaloneSearchBox

分類Dev

Google Maps jQuery Marker not Displaying

分類Dev

Google Maps APIからmarker.setMap(null)を使用してマーカーを削除する方法-JavaScript

分類Dev

Angular Js&google maps API ngGeolocation

分類Dev

Google Maps API Javascript; TypeErroraがnull

分類Dev

How to add marker onClick and show my geolocation in google-maps-react?

分類Dev

Google maps react only displaying marker of last rendered object from state array

分類Dev

How to implement centered marker in Google Maps API v3 with Ionic & Angular

分類Dev

How would I integrate a Google Maps marker input from Javascript into PHP?

分類Dev

Google maps javascript - Marker array, how do I get the lat/lon?

分類Dev

Flutter Image overlay for Google Maps Marker

分類Dev

Removing marker by latitude/longitude in google maps

分類Dev

Adding marker clusterer to google maps with for-loop?

分類Dev

Parse JSON response for Google Maps Marker in Android

分類Dev

Render multiple marker in react-native-maps

分類Dev

check if google maps app is installed in react-native iOS

分類Dev

Flutter google maps and firebase not working together

分類Dev

Google Maps Javascript API、マーカー、ポリラインがReact.jsアプリで一緒に表示されない問題

分類Dev

Google Maps Javascript API tilt view differs from the one on www.google.com/maps

分類Dev

3D Earth globe on google maps API JS

分類Dev

Call google maps api event addListener using dart:js

分類Dev

Google Maps Javascript API204エラー

分類Dev

Google Maps API DeletedApiProjectMapError

分類Dev

Google Maps API

分類Dev

Google Maps Directions API

分類Dev

Google Maps Autocompletion API

分類Dev

Google Maps Angular Js

Related 関連記事

  1. 1

    Plotting multiple polylines on Google Maps

  2. 2

    Google Maps JavaScript API v3 changing marker and add satellite view

  3. 3

    Blazor:Google Maps JS API

  4. 4

    React js google map react-google-maps-api add StandaloneSearchBox

  5. 5

    Google Maps jQuery Marker not Displaying

  6. 6

    Google Maps APIからmarker.setMap(null)を使用してマーカーを削除する方法-JavaScript

  7. 7

    Angular Js&google maps API ngGeolocation

  8. 8

    Google Maps API Javascript; TypeErroraがnull

  9. 9

    How to add marker onClick and show my geolocation in google-maps-react?

  10. 10

    Google maps react only displaying marker of last rendered object from state array

  11. 11

    How to implement centered marker in Google Maps API v3 with Ionic & Angular

  12. 12

    How would I integrate a Google Maps marker input from Javascript into PHP?

  13. 13

    Google maps javascript - Marker array, how do I get the lat/lon?

  14. 14

    Flutter Image overlay for Google Maps Marker

  15. 15

    Removing marker by latitude/longitude in google maps

  16. 16

    Adding marker clusterer to google maps with for-loop?

  17. 17

    Parse JSON response for Google Maps Marker in Android

  18. 18

    Render multiple marker in react-native-maps

  19. 19

    check if google maps app is installed in react-native iOS

  20. 20

    Flutter google maps and firebase not working together

  21. 21

    Google Maps Javascript API、マーカー、ポリラインがReact.jsアプリで一緒に表示されない問題

  22. 22

    Google Maps Javascript API tilt view differs from the one on www.google.com/maps

  23. 23

    3D Earth globe on google maps API JS

  24. 24

    Call google maps api event addListener using dart:js

  25. 25

    Google Maps Javascript API204エラー

  26. 26

    Google Maps API DeletedApiProjectMapError

  27. 27

    Google Maps API

  28. 28

    Google Maps Directions API

  29. 29

    Google Maps Autocompletion API

  30. 30

    Google Maps Angular Js

ホットタグ

アーカイブ