【问题标题】:How to achieve circular marker in google map using react?如何使用反应在谷歌地图中实现圆形标记?
【发布时间】:2021-01-01 03:18:35
【问题描述】:

我正在使用 react-google-maps 来实现地图。 我能够实现默认制造商,特别是 lat 和 lan

但我正在尝试实现圆形标记。我正在使用google.maps.drawing.OverlayType.CIRCLE 来实现圆形标记,但出现错误

您能指导我如何实现这一目标吗?

代码

import React, { Component } from 'react';
import { withGoogleMap, GoogleMap, Marker } from 'react-google-maps';

class HomeComponent extends Component {

    constructor(props) {
        super(props);

        this.state = {
            data: [
                { "name": "Delhi", "coordinates": { lng: 77.1025, lat: 28.7041 }, "mag": 3.3 },
                { "name": "Seoul", "coordinates": { lng: 126.9780, lat: 37.5665 }, "mag": 5.0 },
                { "name": "Shanghai", "coordinates": { lng: 121.4737, lat: 31.2304 }, "mag": 3.3 },
                { "name": "Beijing", "coordinates": { lng: 116.4074, lat: 39.9042 }, "mag": 5.0 },
                { "name": "Mumbai", "coordinates": { lng: 72.8777, lat: 19.0760 }, "mag": 3.3 },
                { "name": "Moscow", "coordinates": { lng: 37.6173, lat: 55.7558 }, "mag": 5.2 },
                { "name": "Dhaka", "coordinates": { lng: 90.4125, lat: 23.8103 }, "mag": 1.5 },
                { "name": "Kolkata", "coordinates": { lng: 88.3639, lat: 22.5726 }, "mag": 4.9 },
                { "name": "Istanbul", "coordinates": { lng: 28.9784, lat: 41.0082 }, "mag": 2.1 },
                { "name": "Tampa", "coordinates": { lng: -82.452606, lat: 27.964157 }, "mag": 3.1 },
                { "name": "canada", "coordinates": { lat: 56.1304, lng: -106.3468 }, "mag": 6.1 },
                { "name": "Karnataka", "coordinates": { lat: 15.3173, lng: 75.7139 }, "mag": 6.1 },
            ]
        }
    }

    render() {
        const GoogleMapExample = withGoogleMap(props => (
            <GoogleMap
                defaultCenter={{ lat: 40.756795, lng: -73.954298 }}
                defaultZoom={4}
            >
                { 
                    this.state.data.map(item =>
                        <Marker
                            icon={{
                                path: google.maps.drawing.OverlayType.CIRCLE,
                                fillColor: 'red',
                                fillOpacity: .2,
                                scale: Math.pow(2, item.mag) / 2,
                                strokeColor: 'white',
                                strokeWeight: .5}
                            }
                            key={item.id}
                            title={item.name}
                            name={item.name}
                            position={{ lat: item.coordinates.lat, lng: item.coordinates.lng }}
                        />
                    )
                }
            </GoogleMap>
        ));
        return (
            <div>
                <GoogleMapExample
                    containerElement={<div style={{ height: `500px`, width: '100%' }} />}
                    mapElement={<div style={{ height: `100%` }} />}
                />
            </div>
        );
    }
}

export default HomeComponent;

这是CodeSandbox的链接

预期输出

【问题讨论】:

  • 您可以在问题中添加sscce 吗?您可以使用CodeSandbox
  • @Pagemag 用 CodeSandbox 更新了我的问题

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


【解决方案1】:

google 并不是真正的undefined,因为您已将其包含在您的index.html 中。事实上,如果您执行console.log(google),您将看到该对象可以正常访问并已定义。

关于圈子,你可以用google.maps.SymbolPath.CIRCLE代替google.maps.drawing.OverlayType.CIRCLE

【讨论】:

  • 好的理解,但是react-google-maps中是否有任何参数可以实现圆形标记?
  • 什么意思?如果您查看我的沙箱,它已经是圆形的并且与您问题底部的图像非常相似。如果您想进一步自定义它,我想您可以更改道具还是您的意思是this 类型的实现?如果需要,你也可以使用custom image data
  • 对不起,我没看到。但我使用google.maps.SymbolPath.CIRCLE 仍然显示谷歌未定义
  • 组件中是否需要加载地图脚本?
  • 关于 google 仍然未定义(尽管我已经解释过它不是 - 它只是 eslint 的工作) - 只需按照我在沙箱上所做的操作并将此评论包含在顶部您的组件:/*global google*/。如果它不适用于您的项目,请使用window.google.maps.SymbolPath.CIRCLE。如果这仍然不起作用,则使用withScriptjs 将脚本加载到组件中而不是index.html。参考此documentation的第三步和第四步
猜你喜欢
  • 2016-04-28
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
相关资源
最近更新 更多