如何在不渲染地图的情况下获取用户位置数据 - Leaflet

How to get user location data without rendering the map - Leaflet

我想将用户位置获取到变量(或会话变量),以便稍后在下一个组件中打印在地图上。

所以,我用这段代码制作了 js 文件:

import L from "leaflet";

let geolocation;
//Make map
let map = L.map('map').fitWorld();

// Locate you
map.locate({setView: true, maxZoom: 16});
function onLocationFound (e) {
    console.log(e);
    geolocation = e;
}
map.on('locationfound', onLocationFound);

function onLocationError (e) {
    alert(e.message);
}

map.on('locationerror', onLocationError);


export default geolocation;

我的任务是获取州的简称,例如: 我在德国,下载位置后需要缩写"DE" 我试着用 "leaflet" 插件来做到这一点,但也许还有其他更好的方法。 并且在执行代码时,我被迫指出将放置 ID 的地图的位置

我找到了解决问题的方法。对于这个“http://ip-api.com/json/

,我将使用 api