在传单矩形内添加文本 [英] Add text inside leaflet rectangle

查看:128
本文介绍了在传单矩形内添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在leaflet映射中创建一个矩形.

I'm using the following code to create a rectangle in the leaflet map.

const rectangles = [[51.49, -0.08], [51.5, -0.06]]   

<Rectangle key={key} bounds={rectangle} color="green">

</Rectangle>

我想在矩形内添加文本,例如矩形的标签,有没有办法做到这一点?

I want to add a text inside the rectangle like a label for the rectangle is there a way to do this?

我正在将反应叶库用于这个.

推荐答案

要在地图上书写,我们可以使用 DivIcon 已添加到React-Leaflet 标记组件.

To write on the map, we can use a DivIcon from the Leaflet library added to a React-Leaflet Marker component.

DivIcon是一个可以包含HTML而不是图像的图标.我们将导入Leaflet库,并使用所需的文本创建一个DivIcon.

A DivIcon is an icon that can contain HTML instead of an image. We'll import the Leaflet library and create a DivIcon with our desired text.

import L from 'leaflet';

const text = L.divIcon({html: 'Your HTML text here'});

将DivIcon添加到标记

创建DivIcon后,我们将其添加到放置在Polygon中心的标记中.

Add the DivIcon to a Marker

With the DivIcon created, we'll add it to a Marker placed in the center of a Polygon.

import React from 'react';
import L from 'leaflet';
import { Marker, Polygon } from 'react-leaflet';

const PolygonWithText = props => {
  const center = L.polygon(props.coord).getBounds().getCenter();
  const text = L.divIcon({html: props.text});

  return(
    <Polygon color="blue" positions={props.coords}>
      <Marker position={center} icon={text} />
    </Polygon>
  );
}

export default PolygonWithText

将标记添加到地图

最后,我们将PolygonMarkerDivIcon添加到Map.

import React, { Component } from 'react';
import {Map, TileLayer} from 'react-leaflet';
import PolygonWithText from './PolygonWithText';

class MyMap extends Component {
  render () {
    return (
      <Map center={[20.75, -156.45]} zoom={13}>
        <TileLayer
          attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
        <PolygonWithText text="MyText" coords={[...]} />
      </Map>
  }
}

export default MyMap;

这篇关于在传单矩形内添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆