如何使用本地png自定义react-google-maps标记?

问题描述 投票:3回答:1

我最近尝试实现tomchentw / react-google-maps,但我似乎无法弄清楚如何切割标记图标,默认情况下,它显示一个红色图标,它工作正常,但当我尝试传入图标道具,没有显示标记。

这是我的代码:

/* global google */
import React, { Component } from 'react';
import { MarkerWithLabel } from 'react-google- 
maps/lib/components/addons/MarkerWithLabel';
import {
  withScriptjs,
  withGoogleMap,
  GoogleMap,
  Marker,
} from 'react-google-maps';
import { compose, withProps, withHandlers } from 'recompose';


const MapWithAMarkerClusterer = compose(
  withProps({
    googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyC3naz5xCZtPlOeMo38InY3GFr4k8A2LO0&v=3.exp&libraries=geometry,drawing,places",
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `100%` }} />,
    mapElement: <div style={{ height: `100%` }} />,
  }),
  withScriptjs,
  withGoogleMap
)(props =>
  <GoogleMap
    defaultZoom={11}
    defaultCenter={{ lat: 25.0391667, lng: 121.525 }}
  >
          <Marker
          position={{ lat: 25.0391667, lng: 121.525 }}
          icon={{
            url: 'assets/image2vector.svg',
            anchor: new google.maps.Point(5, 58),
          }}
        />   
  </GoogleMap>
);

class googleMap extends Component {

  render() {
    return (
      <MapWithAMarkerClusterer />
    )
  }
}

export default googleMap;

如果我删除图标道具,红色标记将返回。但我真的想用自己的本地图标。我怎样才能做到这一点?

reactjs react-google-maps
1个回答
3
投票

您需要直接在渲染中导入或使用require来渲染图像

以下是在React中渲染图像的两种方法。

    import vector from 'assets/image2vector.svg';


    icon={{
        url: vector,
        anchor: new google.maps.Point(5, 58),
      }}

或直接使用require

   icon={{
        url: require('assets/image2vector.svg'),
        anchor: new google.maps.Point(5, 58),
      }}
© www.soinside.com 2019 - 2024. All rights reserved.