英文:
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.
<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",
}}
/>
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 "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);
Replace YOUR_API_KEY, with your API key.
Don't forget to render your map in your application:
import React from "react";
import MapContainer from "./MapContainer";
function App() {
return <MapContainer />;
}
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 "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;
// 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: "red",
fillOpacity: 0.5,
strokeWeight: 1,
strokeColor: "red",
};
// Create a new GeoJSON feature
const feature = {
type: "Feature",
properties: {},
geometry: {
type: "Polygon",
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: "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);
To make the earth gray and other areas a different color:
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: [
// 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],
],
],
},
}}
/>
</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);
答案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: '';
background: url(image file);
position:absolute;
top:0px;
left:0px;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论