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

如何在Folium中绘制从中国到美国的向东折线(默认向西)

Fixing Folium PolyLine to Draw Eastward Route from China to US

Got it, let's tackle this issue head-on. The problem you're facing comes down to how Folium (powered by Leaflet.js) calculates shortest paths on a spherical map. It uses great-circle routes, and when the longitude difference between two points exceeds 180°, it automatically picks the shorter path—which in your case is the westward route through India and the Middle East, instead of the eastward Pacific route you want.

The Solution: Adjust Longitude Values

To force the eastward path, we need to "trick" Folium into recognizing a smaller longitude difference by adding 360° to the US point's negative longitude. This converts the western hemisphere longitude into an equivalent eastern hemisphere value, making the eastward path the shorter one in the calculation.

Modified Code Example

import folium

# Initialize map centered on the Pacific to better visualize the route
my_map = folium.Map(location=[0, 180], zoom_start=2)

# Original coordinates: China (86°E), US (-113°W)
china_coords = [34.4080345, 86.0063832]
# Convert US longitude to positive by adding 360: -113.7621932 + 360 = 246.2378068
usa_coords_adjusted = [36.2044029, -113.7621932 + 360]

# Draw the eastward Pacific route
folium.PolyLine(
    [china_coords, usa_coords_adjusted],
    color="red",
    weight=20,
    opacity=0.8
).add_to(my_map)

# Optional: Add a midpoint (e.g., Hawaii) with adjusted longitude for a smoother path
hawaii_coords_adjusted = [19.898682, -155.665857 + 360]  # -155°W → 204°E
folium.PolyLine(
    [china_coords, hawaii_coords_adjusted, usa_coords_adjusted],
    color="blue",
    weight=10,
    opacity=0.8
).add_to(my_map)

# Save the map to view the result
my_map.save("china_us_east_route.html")

Why This Works

  • Leaflet.js handles longitudes outside the -180° to 180° range by wrapping them around the map, so the adjusted US point will still show up in its correct geographic location.
  • By converting the US longitude to a positive value (>180°), the longitude difference between China (86°E) and the adjusted US point (246°E) drops to ~160°—less than 180°. Folium will now calculate the shortest path along this eastward direction, crossing the Pacific instead of the Atlantic/Indian Oceans.

Note on Midpoints

If you add midpoints along the route, make sure their longitudes are also adjusted to positive values (add 360°) if they're in the western hemisphere. This prevents the path from "snapping back" westward between points.

内容的提问来源于stack exchange,提问作者jovin.pizarro

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:38:52