Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在承诺兑现后让标记显示在谷歌地图上?_Javascript_Reactjs - Fatal编程技术网

Javascript 在承诺兑现后让标记显示在谷歌地图上?

Javascript 在承诺兑现后让标记显示在谷歌地图上?,javascript,reactjs,Javascript,Reactjs,好的,我在我的项目中被困在这一点上,我创建了一个谷歌地图,能够使用foursquare获取一堆位置,并将它们转换为我地图的标记,问题是标记最初不会加载,只有在使用搜索功能后才会显示,我知道我正在异步获取信息,但不知道如何告诉代码在承诺实现后立即加载标记。这是代码 import React, { Component } from 'react'; import { Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps react'

好的,我在我的项目中被困在这一点上,我创建了一个谷歌地图,能够使用foursquare获取一堆位置,并将它们转换为我地图的标记,问题是标记最初不会加载,只有在使用搜索功能后才会显示,我知道我正在异步获取信息,但不知道如何告诉代码在承诺实现后立即加载标记。这是代码

import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps react';
import axios from 'axios';

var  AllPlaces = [

]


axios.get("https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6").then(
response => {
response.data.response.venues.forEach(function(item){
  AllPlaces.push(
    {
      name: item.categories[0].name.toLowerCase(),
      lat: item.location.lat,
      lng: item.location.lng
    }
  )
})
}
)


class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces: []
};

markers = []


onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}

onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}


onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}


CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place => !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)

render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
  <div className = 'navMenu'>
    <div className = 'List'>
      <h1 className = 'title'> Places to Eat
</h1>
        {this.CreateInputField()}
    </div>
    <div className = 'PlaceList'>
      <ol className='Places'>
        {AllPlaces.map((arrayItem, index)=>
        !this.state.filteredPlaces.includes(arrayItem) &&
          <li
          key = {index}
          className='Place'
          onClick={() => {this.onLiClick(index)}}
          >{arrayItem.name}</li>
        )}
      </ol>
    </div>
  </div>
</div>
<Map google={this.props.google} zoom={14}
  initialCenter = {{lat:40.7589, lng:-73.9851}}
  onClick={this.onMapClicked}>
  {AllPlaces.map((marker, i) =>
    !this.state.filteredPlaces.includes(marker) &&
      <Marker
      ref={(e) => {if (e) this.markers[i] = e.marker}}
      onClick={this.onMarkerClick}
      title = {marker.name}
      key = {i}
      name={marker.name}
      position =
 {{lat:marker.lat,lng:marker.lng}}
      />
  )}
  <InfoWindow
    onOpen={this.windowHasOpened}
    onClose={this.windowHasClosed}
    marker={this.state.activeMarker}
    visible={this.state.showingInfoWindow}>
    <div>
      <h1>{this.state.selectedPlace.name}</h1>
    </div>
  </InfoWindow>
 </Map>
 </div>
);
}
}

export default GoogleApiWrapper({
apiKey: 'KEY'
})(MapContainer)

在组件本身内部执行异步请求,然后将该新值放入组件状态。React将知道在状态更改时重新加载,所以您需要担心的只是管理状态。Async/await语法也使它更简洁

e、 g


请修复代码的缩进。我在这里有点困惑,我试图在组件中添加此请求,但认为我可能在状态中添加不正确,基于此,组件状态中的新值是什么?不管怎样,我得到了它,它工作得很好,非常感谢
class MapContainer extends Component {

  async componentWillMount() {
    const axiosData = await axios
      .get(
        'https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6'
      )
      .then(response =>
        response.data.response.venues.map(v => ({
          name: v.categories[0].name.toLowerCase(),
          lat: v.location.lat,
          lng: v.location.lng,
        }))
      );
    this.setState({axiosData})  
  }