【问题标题】:react-google-maps Customize Marker Iconreact-google-maps 自定义标记图标
【发布时间】:2022-04-28 02:14:09
【问题描述】:

目前,我已经成功地将地图组件渲染成一个可重用的组件,使用 InfoModal 显示一个图标,但是,标准的红色谷歌地图图标我没有修改,我想自己创建一个自定义图标。我不确定 ES6 和 JSX 语法我需要做什么。我查看了 react-google-maps 问题并试图查看是否有任何当前或更新的材料可以做到这一点(这可能很简单),但我不确定 react-google-maps 是否有自定义的东西以插件或正确格式创建标记。

import React, { Component } from 'react'
import { withGoogleMap, GoogleMap, Marker, InfoWindow } from 'react-google-maps'
import { DEFAULT_MARKER } from '../../constants/mapDefaults'

const MapGoogleMap = withGoogleMap(props => (
  <GoogleMap
    defaultZoom={16}
    center={props.center}
  >
    {props.markers.map((marker, index) => (
      <Marker
        key={index}
        position={marker.position}
        onClick={() => props.onMarkerClick(marker)}
      >

        {marker.showInfo && (
          <InfoWindow onCloseClick={() => props.onMarkerClose(marker)}>
            <div>{marker.infoContent}</div>
          </InfoWindow>
        )}
      </Marker>
    ))}
  </GoogleMap>
))

export default class Map extends Component {
  state = {
    center: {
      lat: 28.3421135,
      lng: -80.6149092
    },

    markers: [
      {
        position: new google.maps.LatLng(28.3431165, -80.6135908),
        showInfo: false,
        infoContent: (
          <svg
            id="Layer_1"
            xmlns="http://www.w3.org/2000/svg"
            width="16"
            height="16"
            viewBox="0 0 16 16"
          >
            <path
              d="M3.5 0c-1.7 0-3 1.6-3 3.5 0 1.7 1 3 2.3 3.4l-.5 8c0
              .6.4 1 1 1h.5c.5 0 1-.4 1-1L4 7C5.5 6.4 6.5 5 6.5
              3.4c0-2-1.3-3.5-3-3.5zm10 0l-.8 5h-.6l-.3-5h-.4L11
              5H10l-.8-5H9v6.5c0 .3.2.5.5.5h1.3l-.5 8c0 .6.4 1 1 1h.4c.6 0
              1-.4 1-1l-.5-8h1.3c.3 0 .5-.2.5-.5V0h-.4z"
            />
          </svg>
        )
      }, DEFAULT_MARKER
    ]
  }

  handleMarkerClick = this.handleMarkerClick.bind(this);
  handleMarkerClose = this.handleMarkerClose.bind(this);

  handleMarkerClick (targetMarker) {
    this.setState({
      markers: this.state.markers.map(marker => {
        if (marker === targetMarker) {
          return {
            ...marker,
            showInfo: true
          }
        }
        return marker
      })
    })
  }

  handleMarkerClose (targetMarker) {
    this.setState({
      markers: this.state.markers.map(marker => {
        if (marker === targetMarker) {
          return {
            ...marker,
            showInfo: false
          }
        }
        return marker
      })
    })
  }

  render () {
    return (
      <MapGoogleMap
        containerElement={
          <div style={{ height: `500px` }} />
        }
        mapElement={
          <div style={{ height: `500px` }} />
        }
        center={this.state.center}
        markers={this.state.markers}
        onMarkerClick={this.handleMarkerClick}
        onMarkerClose={this.handleMarkerClose}
      />
    )
  }
}

【问题讨论】:

    标签: google-maps


    【解决方案1】:

    我就是这样解决的

    let iconMarker = new window.google.maps.MarkerImage(
                        "https://lh3.googleusercontent.com/bECXZ2YW3j0yIEBVo92ECVqlnlbX9ldYNGrCe0Kr4VGPq-vJ9Xncwvl16uvosukVXPfV=w300",
                        null, /* size is determined at runtime */
                        null, /* origin is 0,0 */
                        null, /* anchor is bottom center of the scaled image */
                        new window.google.maps.Size(32, 32)
                    );
                    return(
                        <Marker
                            icon={iconMarker}
                            key={marker.id}
                            onClick={onClick}
                            position={{ lat: parseInt(marker.latitude), lng: parseInt(marker.longitude)}}>
                            {props.selectedMarker === marker &&
                            <InfoWindow>
                                <div style={{'color':'black'}}>
                                   Shop {marker.name} {stretTexte}
                                </div>
                            </InfoWindow>}
                        </Marker>
                    )
    
    

    【讨论】:

      【解决方案2】:

      富兰克林你可以在反应谷歌地图中使用。它有两个优点,可以在信息窗口或简单的标记组件上使用。我们有图标选项来自定义我们的标记。我们有完全可定制的 div 元素。您只需传递图标网址。标签样式是可选的。欢迎提出任何问题。

      import { MarkerWithLabel } from 'react-google-maps/lib/components/addons/MarkerWithLabel';
      
      var markerStyling= {
        clear: "both", display: "inline-block", backgroundColor: "#00921A", fontWeight: '500',
        color: "#FFFFFF", boxShadow: "0 6px 8px 0 rgba(63,63,63,0.11)", borderRadius: "23px",
        padding: "8px 16px", whiteSpace: "nowrap", width: "160px", textAlign: "center"
      };
      
       <MarkerWithLabel
        key={i}
        position={marker.position}
        labelAnchor={new google.maps.Point(75, 90)}
        labelStyle={markerStyling}
        icon={{
          url: '/build/icon/markPin.svg',
          anchor: new google.maps.Point(5, 58),
        }}
      >
        <div>
          <p> My Marker </p>
        </div>
      </MarkerWithLabel>
      

      【讨论】:

        【解决方案3】:

        在 key={index} 上方缺少标记扩展运算符。这是正确的代码。图标本身是在另一个名为 mapDefaults.js 的文件中定义的,如果有人遇到此问题,请随时与我联系。

        <Marker
                {...marker}
                key={index}
                position={marker.position}
                onClick={() => props.onMarkerClick(marker)}
        >
        

        【讨论】:

        • 嗨,我刚开始尝试自定义图标。我看到您正在使用infoContent 将其传递给 svg 来管理它。同样不会返回新图标。还是那个旧的。你能解释一下你是怎么想出来的吗?谢谢
        • @Neovea 没问题我通过给标记本身一个图标属性并将其作为字符串或作为 SVG 的路径传递一个 url 来自定义图标。 infoContent 用于 InfoWindow,而不是用于标记本身。 const markers = [ { position: {lat: 28.3422, lng: -80.6112}, icon: { path: 'M24.5 9h-8.1l-2.9-7.7C13.4 1.1 13.2 1 13 1c-0.2 0-0.4 0.1-0.5 0.3L9.6 9H1.5C1' // this isn't a correct path due to lack of characters allowed. } }, // or { position: {lat: 28.3422, lng: -80.6112}, icon: importedIcon } ]
        猜你喜欢
        • 2017-04-10
        • 2021-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-05
        • 2018-09-10
        • 2023-03-17
        相关资源
        最近更新 更多