gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:12:24 25 4
gpt4 key购买 nike

我最近尝试实现 tomchentw/react-google-maps,但我似乎不知道如何自定义标记图标,默认情况下,它显示一个红色图标,它工作正常,但是当我尝试传入 icon 属性,没有显示标记。

这是我的代码:

/* 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;

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

最佳答案

您需要在渲染中直接导入或使用 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),
}}

关于reactjs - 如何使用本地png自定义react-google-maps标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52685253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com