需要将react-google-charts中GeoChart的标记从圆形更改为图标。

huangapple go评论90阅读模式
英文:

Need to change markers from circle to icon in react-google-charts GeoChart

问题

我正在使用react-google-charts库进行我的个人项目,并发现有一种方法可以在地图上显示标记。

<Chart
  width={"200%"}
  height={"100%"}
  chartType={"GeoChart"}
  data={geoData}
  rootProps={{ "data-testid": "1" }}
  mapsApiKey={"***My Key***"}
  options={{
    displayMode: "markers",
    sizeAxis: { maxSize: 10, minSize: 10 },
    markerOpacity: 0.9,
    backgroundColor: "none",
    colors: ["#FFA4A5", "#34D399"],
    legend: "none",
    datalessRegionColor: "#343434",
  }}
/>

但默认的标记是圆形的,我想要将其更改为我喜欢的图标。
简而言之,我想要自定义标记,用我的喜欢的图标。

是否有办法在使用react-google-charts时更改/自定义标记?

我查看了geoChart API的选项,但似乎没有相关的选项。

英文:

I am using react-google-charts library for my personal project and found there is a way to display markers on the map.

      &lt;Chart
        width={&quot;200%&quot;}
        height={&quot;100%&quot;}
        chartType=&quot;GeoChart&quot;
        data={geoData}
        rootProps={{ &quot;data-testid&quot;: &quot;1&quot; }}
        mapsApiKey=&quot;***My Key***&quot;
        options={{
          displayMode: &quot;markers&quot;,
          sizeAxis: { maxSize: 10, minSize: 10 },
          markerOpacity: 0.9,
          backgroundColor: &quot;none&quot;,
          colors: [&quot;#FFA4A5&quot;, &quot;#34D399&quot;],
          legend: &quot;none&quot;,
          datalessRegionColor: &quot;#343434&quot;,
        }}
       /&gt;

But the default marker is circle, and then I am gonna change it to my favorite icon.
In a word, I want to customize the marker with my favorite one.

Is there any way to change/customize the markers in using react-google-charts?

I checked the options for geoChart APIs, but it seems there are not such relevant options.

答案1

得分: 0

抱歉,GeoChart不支持自定义标记。

您可以使用google-maps包来实现此功能:

npm install google-maps

在您的代码中:

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

class MapContainer extends Component {
  render() {
    const mapStyles = {
      width: "100%",
      height: "100%",
    };

    return (
      <div style={mapStyles}>
        <GoogleMap
          google={this.props.google}
          zoom={10}
          initialCenter={{ lat: 37.7749, lng: -122.4194 }}
        >
          <Marker
            position={{ lat: 37.7749, lng: -122.4194 }}
            icon={{
              url: "path/to/your/custom/icon.png",
              scaledSize: new window.google.maps.Size(50, 50),
            }}
          />
        </GoogleMap>
      </div>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: "YOUR_API_KEY",
})(MapContainer);

请将"YOUR_API_KEY"替换为您的API密钥。

不要忘记在应用程序中渲染您的地图:

import React from "react";
import MapContainer from "./MapContainer";

function App() {
  return <MapContainer />;
}

export default App;

google-maps-react库本身不直接提供地图上国家或区域的着色支持。但是,您可以直接使用Google Maps JavaScript API来实现此效果。

要在地图上着色国家或区域,您可以使用Google Maps JavaScript API的Data Layer。以下是如何修改您的代码以实现此目的的示例:

import React, { Component } from "react";
import { GoogleApiWrapper, Map, Marker } from "google-maps-react";

class MapContainer extends Component {
  constructor(props) {
    super(props);
    this.mapRef = React.createRef();
  }

  componentDidMount() {
    const { google } = this.props;
    const map = this.mapRef.current.map;

    // 创建新的Data Layer
    const dataLayer = new google.maps.Data();

    // 为Data Layer设置地图
    dataLayer.setMap(map);

    // 定义着色区域的样式
    const areaStyle = {
      fillColor: "red",
      fillOpacity: 0.5,
      strokeWeight: 1,
      strokeColor: "red",
    };

    // 创建新的GeoJSON feature
    const feature = {
      type: "Feature",
      properties: {},
      geometry: {
        type: "Polygon",
        coordinates: [
          // 添加所需国家/区域的坐标
          // 例如,美国的坐标
          [
            [-125, 50],
            [-125, 24],
            [-66, 24],
            [-66, 50],
            [-125, 50],
          ],
        ],
      },
    };

    // 将feature添加到Data Layer
    dataLayer.addGeoJson(feature);

    // 将样式应用于feature
    dataLayer.setStyle(areaStyle);
  }

  render() {
    const mapStyles = {
      width: "100%",
      height: "100%",
    };

    return (
      <div style={mapStyles}>
        <Map
          google={this.props.google}
          zoom={4}
          initialCenter={{ lat: 37.7749, lng: -122.4194 }}
          ref={this.mapRef}
        >
          <Marker
            position={{ lat: 37.7749, lng: -122.4194 }}
            icon={{
              url: "path/to/your/custom/icon.png",
              scaledSize: new window.google.maps.Size(50, 50),
            }}
          />
        </Map>
      </div>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: "YOUR_API_KEY",
})(MapContainer);

要使地球变灰色并使其他区域变为不同的颜色:

import React, { Component } from "react";
import { GoogleApiWrapper, Map, Marker, DataLayer } from "google-maps-react";

class MapContainer extends Component {
  constructor(props) {
    super(props);
    this.mapRef = React.createRef();
  }

  render() {
    const mapStyles = {
      width: "100%",
      height: "100%",
    };

    const areaStyle = {
      fillColor: "red",
      fillOpacity: 0.5,
      strokeWeight: 1,
      strokeColor: "red",
    };

    const earthStyle = {
      fillColor: "gray",
      fillOpacity: 1,
      strokeWeight: 0,
    };

    return (
      <div style={mapStyles}>
        <Map
          google={this.props.google}
          zoom={2}
          initialCenter={{ lat: 0, lng: 0 }}
          ref={this.mapRef}
        >
          <DataLayer>
            <Map
              google={this.props.google}
              zoom={2}
              initialCenter={{ lat: 0, lng: 0 }}
              ref={this.mapRef}
            >
              <DataLayer
                options={{
                  style: earthStyle,
                }}
              />
              <DataLayer
                options={{
                  style: areaStyle,
                }}
                feature={{
                  type: "Feature",
                  properties: {},
                  geometry: {
                    type: "Polygon",
                    coordinates: [
                      // 添加所需国家/区域的坐标
                      // 例如,美国的坐标
                      [
                        [-125, 50],
                        [-125, 24],
                        [-66, 24],
                        [-66, 50],
                        [-125, 50],
                      ],
                    ],
                  },
                }}
              />
            </Map>
          </DataLayer>
          <Marker
            position={{ lat: 37.7749, lng: -122.4194 }}
            icon={{
              url: "path/to/your/custom/icon.png",
              scaledSize: new window.google.maps.Size(50, 50),
            }}
          />
        </Map>
      </div>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: "YOUR_API_KEY",
})(MapContainer);
英文:

Unfortunately GeoChart does not have marker customization support.

You can however use the google-maps package for this:

npm install google-maps

In your code:

 import React, { Component } from &quot;react&quot;;
import { GoogleMap, Marker } from &quot;google-maps-react&quot;;
class MapContainer extends Component {
render() {
const mapStyles = {
width: &quot;100%&quot;,
height: &quot;100%&quot;,
};
return (
&lt;div style={mapStyles}&gt;
&lt;GoogleMap
google={this.props.google}
zoom={10}
initialCenter={{ lat: 37.7749, lng: -122.4194 }}
&gt;
&lt;Marker
position={{ lat: 37.7749, lng: -122.4194 }}
icon={{
url: &quot;path/to/your/custom/icon.png&quot;,
scaledSize: new window.google.maps.Size(50, 50),
}}
/&gt;
&lt;/GoogleMap&gt;
&lt;/div&gt;
);
}
}
export default GoogleApiWrapper({
apiKey: &quot;YOUR_API_KEY&quot;,
})(MapContainer);

Replace YOUR_API_KEY, with your API key.

Don't forget to render your map in your application:

import React from &quot;react&quot;;
import MapContainer from &quot;./MapContainer&quot;;
function App() {
return &lt;MapContainer /&gt;;
}
export default App;

The google-maps-react library itself does not provide direct support for coloring countries or areas on the map. However, you can achieve this effect by utilizing the Google Maps JavaScript API directly.

To color countries or areas on the map, you can use the Data Layer of the Google Maps JavaScript API. Here's an example of how you can modify your code to achieve this:

import React, { Component } from &quot;react&quot;;
import { GoogleApiWrapper, Map, Marker } from &quot;google-maps-react&quot;;
class MapContainer extends Component {
constructor(props) {
super(props);
this.mapRef = React.createRef();
}
componentDidMount() {
const { google } = this.props;
const map = this.mapRef.current.map;
// Create a new Data Layer
const dataLayer = new google.maps.Data();
// Set the map for the Data Layer
dataLayer.setMap(map);
// Define the style for the colored areas
const areaStyle = {
fillColor: &quot;red&quot;,
fillOpacity: 0.5,
strokeWeight: 1,
strokeColor: &quot;red&quot;,
};
// Create a new GeoJSON feature
const feature = {
type: &quot;Feature&quot;,
properties: {},
geometry: {
type: &quot;Polygon&quot;,
coordinates: [
// Add the coordinates for the desired country/area
// For example, the coordinates for the United States
[
[-125, 50],
[-125, 24],
[-66, 24],
[-66, 50],
[-125, 50],
],
],
},
};
// Add the feature to the Data Layer
dataLayer.addGeoJson(feature);
// Apply the style to the feature
dataLayer.setStyle(areaStyle);
}
render() {
const mapStyles = {
width: &quot;100%&quot;,
height: &quot;100%&quot;,
};
return (
&lt;div style={mapStyles}&gt;
&lt;Map
google={this.props.google}
zoom={4}
initialCenter={{ lat: 37.7749, lng: -122.4194 }}
ref={this.mapRef}
&gt;
&lt;Marker
position={{ lat: 37.7749, lng: -122.4194 }}
icon={{
url: &quot;path/to/your/custom/icon.png&quot;,
scaledSize: new window.google.maps.Size(50, 50),
}}
/&gt;
&lt;/Map&gt;
&lt;/div&gt;
);
}
}
export default GoogleApiWrapper({
apiKey: &quot;YOUR_API_KEY&quot;,
})(MapContainer);

To make the earth gray and other areas a different color:

import React, { Component } from &quot;react&quot;;
import { GoogleApiWrapper, Map, Marker, DataLayer } from &quot;google-maps-react&quot;;
class MapContainer extends Component {
constructor(props) {
super(props);
this.mapRef = React.createRef();
}
render() {
const mapStyles = {
width: &quot;100%&quot;,
height: &quot;100%&quot;,
};
const areaStyle = {
fillColor: &quot;red&quot;,
fillOpacity: 0.5,
strokeWeight: 1,
strokeColor: &quot;red&quot;,
};
const earthStyle = {
fillColor: &quot;gray&quot;,
fillOpacity: 1,
strokeWeight: 0,
};
return (
&lt;div style={mapStyles}&gt;
&lt;Map
google={this.props.google}
zoom={2}
initialCenter={{ lat: 0, lng: 0 }}
ref={this.mapRef}
&gt;
&lt;DataLayer&gt;
&lt;Map
google={this.props.google}
zoom={2}
initialCenter={{ lat: 0, lng: 0 }}
ref={this.mapRef}
&gt;
&lt;DataLayer
options={{
style: earthStyle,
}}
/&gt;
&lt;DataLayer
options={{
style: areaStyle,
}}
feature={{
type: &quot;Feature&quot;,
properties: {},
geometry: {
type: &quot;Polygon&quot;,
coordinates: [
// Add the coordinates for the desired country/area
// For example, the coordinates for the United States
[
[-125, 50],
[-125, 24],
[-66, 24],
[-66, 50],
[-125, 50],
],
],
},
}}
/&gt;
&lt;/Map&gt;
&lt;/DataLayer&gt;
&lt;Marker
position={{ lat: 37.7749, lng: -122.4194 }}
icon={{
url: &quot;path/to/your/custom/icon.png&quot;,
scaledSize: new window.google.maps.Size(50, 50),
}}
/&gt;
&lt;/Map&gt;
&lt;/div&gt;
);
}
}
export default GoogleApiWrapper({
apiKey: &quot;YOUR_API_KEY&quot;,
})(MapContainer);

答案2

得分: 0

尝试使用before选择器添加自定义CSS:

.marker-class::before {
    content: '';
    background: url(图片文件);
    position: absolute;
    top: 0px;
    left: 0px;
}
英文:

Can you try to add the custom CSS using before selector?

.marker-class::before{
content: &#39;&#39;;
background: url(image file);
position:absolute;
top:0px;
left:0px;
}

huangapple
  • 本文由 发表于 2023年7月11日 08:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定