移动端获取地理位置经纬度

介绍

本节是非规范的。
地理位置 API 定义了一个高级接口,以仅与托管实现的设备(如纬度和经度)关联的位置信息。API 本身与基础位置信息源无关。常见的位置信息来源包括全球定位系统 (GPS) 和从网络信号(如 IP 地址、RFID、WiFi 和蓝牙 MAC 地址、GSM/CDMA 单元 ID)推断出的位置,以及用户输入。不保证 API 返回设备的实际位置。

成功回调和失败回调

function success(position) {
    const latitude  = position.coords.latitude;
    const longitude = position.coords.longitude;

    status.textContent = '';
    mapLink.href = `https://www.openstreetmap.org/#map=18/${latitude}/${longitude}`;
    mapLink.textContent = `Latitude: ${latitude} °, Longitude: ${longitude} °`;
}

可以通过下面百度地理位置,查看定位的地理位置是否精确

参考:

https://w3c.github.io/geolocation-api/#index-defined-elsewhere
使用地理位置API
百度经纬度反查地理位置

原文地址:https://www.cnblogs.com/geek12/p/13996057.html