gpt4 book ai didi

javascript - ReactJs Leaflet Marker Location位置问题

转载 作者:行者123 更新时间:2023-12-05 08:30:57 25 4
gpt4 key购买 nike

我正在尝试使用传单在我的 reactjs 项目中添加 map ,并希望在 map 上显示车辆的位置。我为此做了一个示例用法。但是当我在 map 上使用标记时,我看到了(正如你在这张图片中看到的):

标记的左上侧贴在我的位置。我想在我的位置中心看到标记的底部,就像圆圈一样。但我做不到。我阅读了 react-leaflet 文档,但没有看到这个参数。如果您帮助我,我将对此表示感谢。谢谢

import React, { Component } from 'react'
import Leaflet from 'leaflet';
import { Map, CircleMarker, TileLayer, Marker, Popup } from 'react-leaflet'
import 'leaflet/dist/leaflet.css';

import icon from 'leaflet/dist/images/marker-icon.png';
import iconShadow from 'leaflet/dist/images/marker-shadow.png';

let DefaultIcon = Leaflet.icon({
iconUrl: icon,
shadowUrl: iconShadow
});

Leaflet.Marker.prototype.options.icon = DefaultIcon;

class SimpleExample extends React.Component {
constructor(props) {
super();
this.state = {
lat: 39.9653301,
lng: 32.7760817,
zoom: 5,
markerPoint: {
x: 320,
y: 192
}
};
this.refMap = React.createRef();
}

render() {
const { lat, lng, zoom } = this.state;
const position = [lat, lng];
return (
<Map ref={this.refMap} center={position} zoom={zoom} style={{ height: '400px', width: '100%' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="&copy; <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
/>
<Marker position={position} draggable="True" pane="popupPane" >
</Marker>
<CircleMarker
center={position}
color='green'
fillColor='red'
radius={20}
fillOpacity={0.5}
stroke={false}
>
<Popup>
<span>A pretty CSS3 popup. <br/> Easily customizable.</span>
</Popup>
</CircleMarker>
</Map>
);
}
}

export default SimpleExample;

最佳答案

定义适当的 iconAnchor大小和标记将放置在正确的位置。

let DefaultIcon = Leaflet.icon({
iconSize: [25, 41],
iconAnchor: [10, 41],
popupAnchor: [2, -40],
iconUrl: icon,
shadowUrl: iconShadow
});

Demo

关于javascript - ReactJs Leaflet Marker Location位置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61179294/

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