百度和谷歌的逆地址解析及GPS、谷歌地图和百度地图坐标之间的转换(python版)...

 

PostGis用函数转换之后的4326坐标为GPS坐标,这个坐标不能直接在谷歌maps的API上面直接使用需要经过上面的转换,因为谷歌地图的国内参考坐标系为GCJ-02,而一般GPS为WGS84的坐标系。而且同一坐标百度和谷歌API逆向地址解析之后的结果不一致,这个需要后续验证:

谷歌:https://maps.googleapis.com/maps/api/geocode/json?latlng=23.082373643343487,113.69356554583203&key=${yourkey}&language=zh-CN

百度:http://api.map.baidu.com/geocoder/v2/?location=lat,lon&output=json&ak=${yourkey}&extensions_road=ture&latest_admin=1&coordtype=wgs84

如果不能翻墙谷歌的话替换maps.googleapis.com为ditu.google.cn。

下面为code:

 

def get_street_baidu(lat_gps, lng_gps):
    # 输入gps坐标,逆地址解析返回百度地图地址信息
    try:
        url = 'http://api.map.baidu.com/geocoder/v2/?&' \
              'location=' + str(lat_gps) + ',' + str(lng_gps) + \
              '&output=json&ak=${yourkey}&extensions_road=ture&coordtype=wgs84ll'
        response = urllib.urlopen(url)
        string = response.read()
        json_data = json.loads(string, encoding='utf8')
        if 'result' in json_data.keys():
            result = json_data['result']
            street = result['addressComponent']['street']
        else:
            street = None
    except Exception as e:
        print(e.message)
    return street


def get_street_google(lat, lng):
    # 输入GPS坐标,返回逆地址解析谷歌地图地址,GPS坐标需要转换成谷歌地图坐标
    try:
        url = 'https://ditu.google.cn/maps/api/geocode/json?latlng=' + \
              str(lat) + ',' + str(lng) + \
              '&key=${yourkey}&language=zh-CN'
        response = urllib.urlopen(url)
        string = response.read()
        json_data = json.loads(string, encoding='utf8')
        if 'results' in json_data.keys():
            result = json_data['results']
            street = result[0]['address_components'][0]['long_name']
        else:
            street = None
    except Exception as e:
        print(e.message)
    return street

转换坐标函数code:

#!/usr/bin/env python
# coding:utf-8
# @author: KaiVen
"""
GPS坐标转换:
WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块)
GCJ-02:中国坐标偏移标准,Google Map、高德、腾讯使用
BD-09:百度坐标偏移标准,Baidu Map使用
"""

import math


def transformLat(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 * math.pi) + 20.0 * math.sin(2.0 * x * math.pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(y * math.pi) + 40.0 * math.sin(y / 3.0 * math.pi)) * 2.0 / 3.0
    ret += (160.0 * math.sin(y / 12.0 * math.pi) + 320 * math.sin(y * math.pi / 30.0)) * 2.0 / 3.0
    return ret


def transformLon(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 * math.pi) + 20.0 * math.sin(2.0 * x * math.pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(x * math.pi) + 40.0 * math.sin(x / 3.0 * math.pi)) * 2.0 / 3.0
    ret += (150.0 * math.sin(x / 12.0 * math.pi) + 300.0 * math.sin(x / 30.0 * math.pi)) * 2.0 / 3.0
    return ret


def delta(lat, lng):
    a = 6378245.0
    # a: 卫星椭球坐标投影到平面地图坐标系的投影因子
    ee = 0.00669342162296594323
    # ee: 椭球的偏心率
    dLat = transformLat(lng - 105.0, lat - 35.0)
    dLon = transformLon(lng - 105.0, lat - 35.0)
    radLat = lat / 180.0 * math.pi
    magic = math.sin(radLat)
    magic = 1 - ee * magic * magic
    sqrtMagic = math.sqrt(magic)
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * math.pi)
    dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * math.pi)
    return dLat, dLon


def wgs2gcj(wgsLat, wgsLng):
    """
    WGS-84转成GCJ-02
    """
    if outOfChina(wgsLat, wgsLng):
        print("The latitude or longitude is out of China!")
        return wgsLat, wgsLng
    lat, lng = delta(wgsLat, wgsLng)
    return wgsLat + lat, wgsLng + lng


def gcj2wgs_rough(gcjLat, gcjLon):
    """
    GCJ-02 转 WGS-84 粗略版
    """
    if outOfChina(gcjLat, gcjLon):
        print("The latitude or longitude is out of China!")
        return gcjLat, gcjLon
    lat, lng = delta(gcjLat, gcjLon)
    return gcjLat - lat, gcjLon - lng


def gcj2wgs_accurate(gcjLat, gcjLon):
    """
    GCJ-02 转 WGS-84 精确版
    """
    initDelta = 0.01
    threshold = 0.000000001
    dLat = initDelta
    dLon = initDelta
    mLat = gcjLat - dLat
    mLon = gcjLon - dLon
    pLat = gcjLat + dLat
    pLon = gcjLon + dLon
    wgsLat = 0
    wgsLon = 0
    i = 0
    while 1:
        wgsLat = (mLat + pLat) / 2
        wgsLon = (mLon + pLon) / 2
        lat, lon = gcj2wgs_rough(wgsLat, wgsLon)
        dLat = lat - gcjLat
        dLon = lon - gcjLon
        if (abs(dLat) < threshold) and (abs(dLon) < threshold):
            break
        if dLat > 0:
            pLat = wgsLat
        else:
            mLat = wgsLat
        if dLon > 0:
            pLon = wgsLon
        else:
            mLon = wgsLon
        if ++i > 10000:
            break
    return wgsLat, wgsLon


def gcj2bd(gcjLat, gcjLon):
    """
    GCJ-02 转 BD-09
    """
    x_pi = math.pi * 3000.0 / 180.0
    x = gcjLon
    y = gcjLat
    z = math.sqrt(x * x + y * y) + 0.00002 * math.sin(y * x_pi)
    theta = math.atan2(y, x) + 0.000003 * math.cos(x * x_pi)
    bdLon = z * math.cos(theta) + 0.0065
    bdLat = z * math.sin(theta) + 0.006
    return bdLat, bdLon


def bd2gcj(bdLat, bdLon):
    """
    BD-09 转 GCJ-02
    """
    x_pi = math.pi * 3000.0 / 180.0
    x = bdLon - 0.0065
    y = bdLat - 0.006
    z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * x_pi)
    theta = math.atan2(y, x) - 0.000003 * math.cos(x * x_pi)
    gcjLon = z * math.cos(theta)
    gcjLat = z * math.sin(theta)
    return gcjLat, gcjLon


def wgs2mercator(wgsLat, wgsLon):
    """
    WGS-84 to Web mercator
    mercatorLat -> y mercatorLon -> x
    """
    x = wgsLon * 20037508.34 / 180.
    y = math.log(math.tan((90. + wgsLat) * math.pi / 360)) / (math.pi / 180)
    y = y * 20037508.34 / 180.
    return y, x


def mercator2wgs(mercatorLat, mercatorLon):
    """
    Web mercator to WGS-84
    mercatorLat -> y mercatorLon -> x
    """
    x = mercatorLon / 20037508.34 * 180
    y = mercatorLat / 20037508.34 * 180
    y = 180 / math.pi * (2 * math.atan(math.exp(y * math.pi / 180.)) - math.pi / 2)
    return y, x


def outOfChina(lat, lng):
    """
    判断是否在中国范围外
    """
    if lng < 72.004 or lng > 137.8347:
        return True
    if lat < 0.8293 or lat > 55.8271:
        return True
    return False


def haversine(lat1, lon1, lat2, lon2):
    """
    :param: 纬度1,经度1,纬度2,经度2(十进制度数)
    :return: 二个坐标之间的距离(单位米)
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    """
    # 将十进制度数转化为弧度
    lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])
    # haversine公式
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
    c = 2 * math.asin(math.sqrt(a))
    r = 6371  # 地球平均半径,单位为公里
    return c * r * 1000

坐标转换参考:https://www.oschina.net/code/snippet_260395_39205

转载于:https://www.cnblogs.com/Kaivenblog/p/8607803.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
maptalks是一个基于WebGL的开源地图库,支持加载多种地图数据源,包括百度地图谷歌地图。在使用maptalks加载百度地图谷歌地图时,需要进行坐标转换,以下是一个简单的示例代码: ``` // 创建百度地图图层 var baiduLayer = new maptalks.TileLayer('baidu', { 'urlTemplate': 'http://online{s}.map.bdimg.com/onlinelabel/', 'subdomains': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'attribution': '© Baidu Map' }); // 创建谷歌地图图层 var googleLayer = new maptalks.TileLayer('google', { 'urlTemplate': 'http://mt{s}.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&x={x}&y={y}&z={z}', 'subdomains': ['0', '1', '2', '3'], 'attribution': '© Google Map' }); // 百度地图坐标转换谷歌地图坐标 function baiduToGoogle(lng, lat) { var x_pi = 3.14159265358979324 * 3000.0 / 180.0; var x = lng - 0.0065; var y = lat - 0.006; var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi); var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi); var googleLng = z * Math.cos(theta); var googleLat = z * Math.sin(theta); return [googleLng, googleLat]; } // 加载百度地图 var map = new maptalks.Map('map', { 'center' : [116.404, 39.915], 'zoom' : 14, 'baseLayer' : baiduLayer }); // 监听地图移动事件,实时更新坐标转换 map.on('moveend', function() { var center = map.getCenter(); var googleCenter = baiduToGoogle(center.x, center.y); googleLayer.config('urlTemplate', 'http://mt{s}.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&x={x}&y={y}&z={z}'); map.setBaseLayer(googleLayer); map.setCenter(googleCenter); }); ``` 上述代码中,我们首先创建了百度地图图层和谷歌地图图层,然后编写了一个baiduToGoogle函数,用于将百度地图坐标转换谷歌地图坐标。接着,我们创建了一个maptalks地图,并将百度地图图层作为底图。在地图移动结束后,我们获取地图中心点的坐标,并进行坐标转换,然后使用谷歌地图图层替换百度地图图层。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值