You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在echarts4r中为地图点位添加指定半径范围圈

在echarts4r中为地图点位添加50km范围圈

你可以通过sf包生成地理缓冲区,再结合e_polygon_g将范围圈添加到echarts地图中。核心思路是先把经纬度点位转换为支持米单位的投影,生成缓冲区后再转回WGS84坐标系,最后提取坐标数据传给echarts。

以下是完整的可运行代码:

library(tidyverse)
library(echarts4r)
library(sf)
library(httr)

# 获取荷兰地图数据
url <- paste0("https://service.pdok.nl/cbs/gebiedsindelingen/2024/wfs/v1_0?request=GetFeature&service=WFS&typeName=gebiedsindelingen:gemeente_gegeneraliseerd&outputFormat=json&version=2.0.0&srsname=urn:ogc:def:crs:EPSG::4326")
geo_data <- httr::GET(url) %>% httr::content()  

# 定义点位数据
locations <- data.frame(
  name = c("Amsterdam", "Utrecht"),
  lon = c(4.9041, 5.1214),
  lat = c(52.3676, 52.0907)
)  

# 生成50km缓冲区数据
buffer_data <- locations %>%
  # 转换为sf空间对象(WGS84坐标系)
  st_as_sf(coords = c("lon", "lat"), crs = 4326) %>%
  # 转换为荷兰本地投影(EPSG:28992,单位为米)
  st_transform(crs = 28992) %>%
  # 生成50000米(50km)缓冲区
  st_buffer(dist = 50000) %>%
  # 转回WGS84坐标系
  st_transform(crs = 4326) %>%
  # 提取缓冲区的坐标列表,整理为echarts需要的格式
  mutate(
    coords = map(geometry, function(x) {
      x %>% st_coordinates() %>% as.data.frame() %>% select(X, Y) %>% as.matrix() %>% list()
    })
  ) %>%
  unnest(coords)

# 绘制带范围圈的地图
locations |> 
  e_charts(lon) |> 
  echarts4r::e_map_register("NL", geo_data) |> 
  e_geo(map = "NL", nameProperty = "statnaam") |> 
  # 添加范围圈
  e_polygon_g(
    data = buffer_data$coords,
    name = "50km范围",
    coord_system = "geo",
    itemStyle = list(
      color = "rgba(255, 100, 100, 0.2)",  # 填充透明度
      borderColor = "#ff6464",             # 边框颜色
      borderWidth = 1
    )
  ) |> 
  # 添加点位
  e_scatter(lat, name = "City", coord_system = "geo", symbol_size = 8)

关键步骤说明:

  • 投影转换:WGS84(EPSG:4326)是经纬度坐标系,无法直接用米计算距离,所以先转换为荷兰本地的UTM投影(EPSG:28992),生成缓冲区后再转回原坐标系。
  • 缓冲区生成:st_buffer(dist = 50000)直接生成50km半径的圆形范围。
  • 坐标整理:通过st_coordinates提取缓冲区的边界坐标,转换为echarts4r需要的嵌套列表格式。
  • 样式设置:通过itemStyle调整范围圈的填充透明度和边框样式,避免遮挡地图和点位。

内容的提问来源于stack exchange,提问作者Quinten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 13:19:55