首页  编辑  

通过基站LAC和Cell ID查询基站经纬度坐标

Tags: /Ruby/   Date Created:
下面的方法未经证实
import binascii
import requests
import struct
import sys

def queryGlmMmap(mcc, mnc, lac, cellid):
    a = '000E00000000000000000000000000001B0000000000000000000000030000'
    b = hex(cellid)[2:].zfill(8) + hex(lac)[2:].zfill(8)
    c = hex(mnc)[2:].zfill(8) + hex(mcc)[2:].zfill(8)
    string = binascii.unhexlify(a + b + c + 'FFFFFFFF00000000')

    try:
        response = requests.post('http://www.google.com/glm/mmap', string, timeout=5)
        if 25 == len(response.content):
            (a, b, errorCode, lat, lon, rng, c, d) = struct.unpack(">hBiiiiih", response.content)
            lat = lat / 1000000.0
            lon = lon / 1000000.0
            if 0 == errorCode:
                print('{0}|{1}|{2}'.format(lat, lon, rng))
            else:
                print('Error:', errorCode)
        else:
            print('No match')
    except Exception as e:
        print('Error:', e)

queryGlmMmap(int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]))

import requests
import json

url = "https://www.google.com/glm/mmap"

data = {
    "version": "1.1.0",
    "host": "www.google.com",
    "access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",
    "request_address": True,
    "address_language": "zh_CN",
    "cell_towers": [
        {
            "cell_id": 11308,
            "location_area_code": 4269,
            "mobile_country_code": 460,
            "mobile_network_code": 0
        }
    ]
}

headers = {
    "Content-Type": "application/json"
}

# 发送 POST 请求
response = requests.post(url, data=json.dumps(data), headers=headers)

# 打印响应状态码和内容
print(f"Status Code: {response.status_code}")
print("Response:")
print(response.text)

下面的方法已经失效(Update: 2024-1-1)
现在LBS服务很多了,大部分都是通过LAC和小区ID(Cell ID)来实现的,Google有提供LBS的API接口,我们只要往
www.google.com 的HTTP端口(80)发送类似下面的数据包,就可以获取到基站的经纬度坐标:
POST /loc/json HTTP/1.1
Host: www.google.com
Accpet: */*
Content-Type: application/json
Content-Length: 201

{
"version": "1.1.0",
"request_address": false,
"cell_towers": [
{
"location_area_code":4269,
"cell_id":11308,
"mobile_network_code": 460,
"mobile_country_code": 0
}
]
}

返回的是个JSON数据:

{

"location": {

"latitude":39.9225332,

"longitude":116.434117,

"accuracy":533.0

},

"access_token":"2:lDcazDiw_ijl1Kzm:M96T4vAKlkCvNS-G"

}

Ruby的实现代码如下:

require 'net/http'
require 'json'

# Get site location by LAC,CELL_ID
# example:
# get_gps_by_cell(4269, 11308) ==> [116.434117, 39.9225332]
def get_gps_by_cell(lac, cell_id, mcc = nil, mnc = nil)
request = Net::HTTP::Post.new("/loc/json")
request.content_type = 'application/json'
request.body = JSON::generate({
:version => '1.1.0',
:request_address => false,
:cell_towers => [
{:location_area_code => lac, :cell_id => cell_id, :mobile_network_code => mnc, :mobile_country_code => mcc}
]
})

net = Net::HTTP.new("www.google.com", 80)
response = net.start { |http| http.request(request) }
return nil if response.code != "200"
location = JSON.parse(response.body)["location"]
[location["longitude"], location["latitude"]]
end

调用例子:

get_gps_by_cell(4269, 11308)