分享一个WGS84与GCJ02互相转换的代码,需要的自取哈~
import tkinter as tk
from tkinter import ttk
import math
# 常量
PI = 3.1415926535897932384626
A = 6378245.0
EE = 0.00669342162296594323
def gcj02_to_wgs84(lon, lat):
"""将 GCJ-02 坐标转换为 WGS-84 坐标"""
if out_of_china(lon, lat):
return lon, lat
dlat = _transform_lat(lon - 105.0, lat - 35.0)
dlon = _transform_lon(lon - 105.0, lat - 35.0)
radlat = lat / 180.0 * PI
magic = math.sin(radlat)
magic = 1 - EE * magic * magic
sqrtmagic = math.sqrt(magic)
dlat = (dlat * 180.0) / ((A * (1 - EE)) / (magic * sqrtmagic) * PI)
dlon = (dlon * 180.0) / (A / sqrtmagic * math.cos(radlat) * PI)
mg_lat = lat + dlat
mg_lon = lon + dlon
# 计算偏移量
offset_lat = mg_lat - lat
offset_lon = mg_lon - lon
# WGS-84 校正
wgs_lat = lat - offset_lat
wgs_lon = lon - offset_lon
return wgs_lon, wgs_lat
def wgs84_to_gcj02(lon, lat):
"""将 WGS-84 坐标转换为 GCJ-02 坐标"""
if out_of_china(lon, lat):
return lon, lat
dlat = _transform_lat(lon - 105.0, lat - 35.0)
dlon = _transform_lon(lon - 105.0, lat - 35.0)
radlat = lat / 180.0 * PI
magic = math.sin(radlat)
magic = 1 - EE * magic * magic
sqrtmagic = math.sqrt(magic)
dlat = (dlat * 180.0) / ((A * (1 - EE)) / (magic * sqrtmagic) * PI)
dlon = (dlon * 180.0) / (A / sqrtmagic * math.cos(radlat) * PI)
mglat = lat + dlat
mglon = lon + dlon
return mglon, mglat
def _transform_lat(x, y):
"""计算纬度偏移量"""
ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x))
ret += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0
ret += (20.0 * math.sin(y * PI) + 40.0 * math.sin(y / 3.0 * PI)) * 2.0 / 3.0
ret += (160.0 * math.sin(y / 12.0 * PI) + 320 * math.sin(y * PI / 30.0)) * 2.0 / 3.0
return ret
def _transform_lon(x, y):
"""计算经度偏移量"""
ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x))
ret += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0
ret += (20.0 * math.sin(x * PI) + 40.0 * math.sin(x / 3.0 * PI)) * 2.0 / 3.0
ret += (150.0 * math.sin(x / 12.0 * PI) + 300.0 * math.sin(x / 30.0 * PI)) * 2.0 / 3.0
return ret
def out_of_china(lon, lat):
"""判断是否在中国境外"""
if lon < 72.004 or lon > 137.8347:
return True
if lat < 0.8293 or lat > 55.8271:
return True
return False
def convert_coordinates():
"""转换坐标并更新结果"""
try:
input_lon = float(entry_lon.get())
input_lat = float(entry_lat.get())
source_crs = source_crs_var.get()
target_crs = target_crs_var.get()
# 选择转换
if source_crs == "WGS-84(GPS)" and target_crs == "GCJ-02(高德、腾讯)":
lon, lat = wgs84_to_gcj02(input_lon, input_lat)
elif source_crs == "GCJ-02(高德、腾讯)" and target_crs == "WGS-84(GPS)":
lon, lat = gcj02_to_wgs84(input_lon, input_lat)
else:
raise ValueError("不支持的坐标系统转换")
# 更新结果显示
result_text.set(f"{lon:.6f} {lat:.6f}")
except Exception as e:
result_text.set(f"错误: {str(e)}")
def copy_to_clipboard():
"""将结果复制到剪贴板"""
root.clipboard_clear()
root.clipboard_append(result_text.get())
# 创建主窗口
root = tk.Tk()
root.title("坐标转换器")
# 输入字段
tk.Label(root, text="经度:").grid(row=0, column=0, padx=5, pady=5)
tk.Label(root, text="纬度:").grid(row=1, column=0, padx=5, pady=5)
entry_lon = tk.Entry(root)
entry_lat = tk.Entry(root)
entry_lon.grid(row=0, column=1, padx=5, pady=5)
entry_lat.grid(row=1, column=1, padx=5, pady=5)
# 选择坐标系统
tk.Label(root, text="源坐标系统:").grid(row=2, column=0, padx=5, pady=5)
tk.Label(root, text="目标坐标系统:").grid(row=3, column=0, padx=5, pady=5)
source_crs_var = tk.StringVar(value="GCJ-02(高德、腾讯)")
target_crs_var = tk.StringVar(value="WGS-84(GPS)")
source_crs_menu = ttk.Combobox(root, textvariable=source_crs_var, values=["WGS-84(GPS)", "GCJ-02(高德、腾讯)"],
state="readonly")
target_crs_menu = ttk.Combobox(root, textvariable=target_crs_var, values=["WGS-84(GPS)", "GCJ-02(高德、腾讯)"],
state="readonly")
source_crs_menu.grid(row=2, column=1, padx=5, pady=5)
target_crs_menu.grid(row=3, column=1, padx=5, pady=5)
# 结果显示
result_text = tk.StringVar()
result_label = tk.Text(root, height=2, width=40, wrap="word", bg="white", relief="sunken")
result_label.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
def update_result_label(*args):
result_label.delete(1.0, tk.END)
result_label.insert(tk.END, result_text.get())
result_text.trace_add("write", update_result_label)
# 转换按钮
convert_button = tk.Button(root, text="转换", command=convert_coordinates)
convert_button.grid(row=4, column=0, columnspan=2, padx=5, pady=5)
# 复制按钮
copy_button = tk.Button(root, text="复制结果", command=copy_to_clipboard)
copy_button.grid(row=6, column=0, columnspan=2, padx=5, pady=5)
# 运行主事件循环
root.mainloop()