我最近尝试实现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;
如果我删除图标道具,红色标记将返回。但是我真的很想使用我自己的本地图标。我该怎么办?
答案 0 :(得分: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),
}}