繁体   English   中英

ReactJs Leaflet 标记位置 Position 问题

[英]ReactJs Leaflet Marker Location Position Problem

我正在尝试使用 leaflet 在我的 reactjs 项目中添加 map 并希望在 Z144518AEZEFE45 上显示车辆的位置。 我为此做了一个示例。 但是当我在 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
});

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM