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

如何使用ggmap绘制以指定坐标为圆心、公里/英里为半径的范围圆

ggmap添加指定实际距离半径范围圆实现方法

由于经纬度是球面坐标,直接基于坐标差值画圆会出现变形、距离不准的问题,需要基于球面距离计算圆周点坐标,具体实现如下:

依赖包

需要用到geosphere包计算球面距离对应的经纬度点,首次使用请先安装:

install.packages("geosphere")
library(ggmap)
library(geosphere)

核心实现代码

1. 生成圆周坐标

通过destPoint()函数以圆心为起点,沿360个方向计算指定半径位置的经纬度,拼接为闭合多边形即可得到准确的范围圆:

# 圆心与半径参数
center_lon <- 3.0818176532603374
center_lat <- 45.7765976040585
radius_m <- 500 

# *单位换算规则,按需修改半径值即可*
# 公里转米:radius_m = 公里数 * 1000
# 英里转米:radius_m = 英里数 * 1609.34

# 计算圆周点
circle_pts <- destPoint(
  p = c(center_lon, center_lat),
  b = 1:360, # 每1度取一个点,足够保证圆形平滑
  d = radius_m
)
# 转换为数据框并闭合多边形
circle_df <- as.data.frame(circle_pts)
circle_df <- rbind(circle_df, circle_df[1,])

2. 叠加到ggmap地图

把生成的圆通过ggplot的几何图层叠加到底图上即可,完整绘图代码:

# 原有底图获取代码
town <- get_stamenmap(
  bbox = c(left = 3.07628, bottom = 45.77407, right = 3.08797, top = 45.77905),
  maptype = "terrain",
  zoom = 16
) 

# 原有圆心坐标定义
town_cor <- data.frame(
  lat = 45.7765976040585,
  long = 3.0818176532603374
)

# 最终绘图
ggmap(town) +
  # 绘制范围圆,可自行调整颜色、透明度、线宽参数
  geom_polygon(
    data = circle_df,
    aes(x = lon, y = lat),
    fill = "#1E90FF",
    color = "darkblue",
    alpha = 0.25,
    linewidth = 1
  ) +
  # 标记圆心位置,可按需调整样式
  geom_point(
    data = town_cor,
    aes(x = long, y = lat),
    color = "red",
    size = 2.5
  )

避坑提示

  • 不要直接使用ggforce::geom_circle绘制范围圆,该函数基于平面笛卡尔坐标系计算半径,在经纬度投影下会出现椭圆变形,且实际距离与设定值偏差极大。
  • 如果需要更平滑的圆形边缘,可将角度序列调整为更小的步长,例如b = seq(0, 360, by = 0.5)即可获得更高精度的圆周。
  • 该方法生成的圆为WGS84坐标系下的真实球面距离圆,和实际地理范围完全匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 01:21:41