根据地址获取坐标经纬度

本文介绍如何根据地址查询对应的经纬度坐标,例如查询北京市西城区西长安街2号或国家大剧院的位置。通过提供的代码,可以实现从地址到坐标转换的功能。
摘要由CSDN通过智能技术生成

根据地址获取经纬度坐标

根据地址获取坐标经纬度

根据地址查经纬度坐标

目标:根据提供的一串地址的文字描述,查出此地址对应的经纬度。

例如你要查询『北京市西城区西长安街2号 』或『国家大剧院』的坐标。

                                                                                                

当然你也可以直接运行本文的代码,我们会得到一个坐标

81.84627200861524, 43.2213079855618,把此坐标粘贴到坐标拾取网站:拾取坐标系统

你会看到一个八卦城。

                                   

话不多说,代码如下:

# 根据文字查坐标
import json
from urllib.request import urlopen, quote
import requests
# 定义一个函数
def getlnglat(address):
    url = 'https://api.map.baidu.com/geocoding/v3/'
    output = 'json'
    ak = '你申请的ak' # 你申请的ak
    address = quote(address) # 由于本文地址变量为中文,为防止乱码,先用quote进行编码
    uri = url + '?' + 'address=' + address  + '&output=' + output + '&ak=' + ak +""
    req = urlopen(uri)
    res = req.read().decode() 
    print(res)
    temp = json.loads(res)
    result = temp['result']
    lat = result['location']['lat']
    lng = result['location']['lng']
    print(result['precise'],result['confidence'],result['comprehension'],result['level'])
    return (lat, lng),(lng,lat)
getlnglat('特克斯县的中心太极坛')

在 Electron 中使用 Geolocation 获取坐标经纬度可以通过以下步骤实现: 1. 在渲染进程(即网页)中,使用 `navigator.geolocation.getCurrentPosition()` 方法获取地理位置信息。该方法接受两个参数:成功回调函数和失败回调函数。 2. 成功回调函数将会接收一个 `Position` 对象作为参数,该对象包含有关用户当前位置的信息,如经度、纬度、海拔高度等。 3. 失败回调函数将会接收一个 `PositionError` 对象作为参数,该对象包含有关获取位置信息失败的信息,如错误代码和错误消息等。 以下是一个简单的示例代码,用于在 Electron 中获取用户当前位置的经度和纬度: ```javascript // index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Electron Geolocation Example</title> </head> <body> <h1>Electron Geolocation Example</h1> <p>Latitude: <span id="latitude"></span></p> <p>Longitude: <span id="longitude"></span></p> <script> function showPosition(position) { document.getElementById("latitude").innerHTML = position.coords.latitude; document.getElementById("longitude").innerHTML = position.coords.longitude; } function showError(error) { console.log(error.message); } navigator.geolocation.getCurrentPosition(showPosition, showError); </script> </body> </html> // main.js const { app, BrowserWindow } = require('electron') function createWindow () { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }) win.loadFile('index.html') } app.whenReady().then(() => { createWindow() }) ``` 在上面的示例中,`getCurrentPosition()` 方法将 `showPosition()` 函数作为成功回调函数,将 `showError()` 函数作为失败回调函数。在 `showPosition()` 函数中,我们将获取的经度和纬度信息显示在页面上。在 `showError()` 函数中,我们将错误信息输出到控制台上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只要开始永远不晚

谢谢打赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值