【问题标题】:How to display results[0].formatted_address in InfoWindow如何在 InfoWindow 中显示 results[0].formatted_address
【发布时间】:2018-05-26 00:39:28
【问题描述】:

我现在正在制作一个带有 react 的谷歌地图,但我的 placeId 显示结果有问题。我有这个代码:

<Marker
    {...marker}
    key={i}
    position={marker.location}
    title={marker.title}
    icon={'icons/' + marker.type + '.png'}
    onClick={() => {
      props.toggleLocationsActive(i);
    }}
    //animation={DROP}
    >
    {i === props.activeKey && (
       geocodeByPlaceId(marker.place_id)
          .then(results => {
             const address = results[0].formatted_address;
             console.log(address);
          })
          .catch(error => console.error(error)),
       <InfoWindow onCloseClick={props.onToggleOpen}>
            <div>{ marker.title }</div>
       </InfoWindow>)}
   </Marker>

我想在 InfoWindow 中显示取自这个 placeId 的地址 - 并显示 {marker.title}。而且我不知道该怎么做...这是GitHub存储库上该项目的链接:https://github.com/hajczek/Neighborhood---Warsaw-Cultural-Map也许你们中的一些人知道,如何解决这个问题...感谢您的任何回答:)

【问题讨论】:

  • 我通过这种方式解决了这个问题:document.getElementById('address').textContent = address;

标签: reactjs google-maps google-places react-google-maps


【解决方案1】:

我通过这种方式解决了这个问题:

document.getElementById('address').textContent = address;

我把这段代码放在'then'的一部分:

.then(results => {
     const address = results[0].formatted_address;
     console.log(address);
     document.getElementById('address').textContent = address;
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-29
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多