【问题标题】:react google maps icons not displayed反应谷歌地图图标未显示
【发布时间】:2018-11-02 04:16:46
【问题描述】:

我在谷歌地图上显示图标时遇到问题。 这是我的代码:

import React, { Component } from 'react'
import { compose, withProps, withStateHandlers } from 'recompose'
import { withScriptjs, withGoogleMap, GoogleMap, Marker, InfoWindow } from 'react-google-maps'
import MapStyles from './data/MapStyles'

export const Map = compose(
    withStateHandlers(() => ({
    isOpen: false,
  }), {
    onToggleOpen: ({ isOpen }) => () => ({
      isOpen: !isOpen,
    })
  }),
  withScriptjs,
  withGoogleMap
)(props => {
        return (
        <GoogleMap
            defaultZoom={13}
            defaultCenter={{ lat: 52.229676, lng: 21.012229 }}
            defaultOptions={{ styles: MapStyles }}
            mapTypeControl={false}
        >{ props.markers.map(marker => {
            console.log(marker.location);
            <Marker
                {...marker}
                key={marker.place_id}
                position={marker.location}
                title={marker.title}
                icon={{
                    url: 'icons/icon.png'
                  }}
                onClick={props.onToggleOpen}
                >
                {props.isOpen && 
                <InfoWindow onCloseClick={props.onToggleOpen}>
                    <div>Open</div>
                </InfoWindow>}
            </Marker>
        })}
        </GoogleMap>

);})

export default Map

console.log 会在控制台中显示标记的位置,但它们不会显示在地图上……我不知道为什么……也许你们中有些人知道,为什么会发生这种情况……这是 Github 上该项目的链接:https://github.com/hajczek/Neighborhood---Warsaw-Cultural-Map

感谢任何提示。

【问题讨论】:

  • MapStyles 下的内容
  • 这是带有地图样式的文件...

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


【解决方案1】:

您在地图投影中缺少return -

>{ props.markers.map(marker => {
            console.log(marker.location);
            <Marker

&lt;Marker之前添加return

>{ props.markers.map(marker => {
            console.log(marker.location);
            return <Marker

codesandbox 上的工作示例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多