使用 Azure 地图获取用户的当前位置

问题描述 投票:0回答:3

我正在开发一个 Django 项目,当用户按下某个按钮时,我需要用户的当前位置。
我需要借助 Azure Maps 来完成此操作。我已阅读他们的文档。尽管如此,我仍然无法找到获取用户位置的方法。
我想要的是,当用户按下按钮时,Azure 地图 API 必须返回用户的位置。
1. 是否可以使用Azure地图获取用户的位置?
2. 如果是,我该如何开始(任何资源/文档)?

django azure geolocation azure-maps
3个回答
0
投票

在浏览器中获取用户位置可以使用浏览器内置的地理定位 API 来完成,无需使用任何其他库,例如 Azure Maps。如果 Azure Maps 要提供这样的功能,他们将简单地包装此内置功能(这也是其他地图平台所做的)。这是一些文档:https://www.w3schools.com/html/html5_geolocation.asp

以下是在 Azure Maps 中使用地理定位 API 的代码示例:https://azuremapscodesamples.azurewebsites.net/#device-sensors


0
投票

这个文档非常有帮助。您可以提出多个请求,其中之一根据纬度和经度返回地址。
这是一个例子:

GET https://atlas.microsoft.com/search/address/reverse/{format}?subscription-key={subscription-key}&api-version=1.0&query={query}

只需插入值(格式、订阅密钥等)。

format:所需的响应格式。值可以是 json 或 xml。

订阅密钥:Azure Maps 订阅密钥(您将通过您的 Azure 地图帐户获取此密钥)。

查询:适用的查询指定为逗号分隔的字符串,由纬度后跟经度组成,例如“47.641268,-122.125679”。

您可以使用上面的 url(插入值)来获取所需的响应。


0
投票

您可以像这样在自定义控件上添加事件侦听器。如何将按钮添加到地图上?我还没有弄清楚这一点,也没有找到这样做的例子。

//If the user presses the My Location button, use the geolocation API to get the users location and center/zoom the map to that location.
    document.getElementById('myLocationBtn').onclick = setMapToUserLocation;

function setMapToUserLocation() {
    //Request the user's location.
    navigator.geolocation.getCurrentPosition(function (position) {
        //Convert the geolocation API position into a longitude/latitude position value the map can understand and center the map over it.
        map.setCamera({
            center: [position.coords.longitude, position.coords.latitude],
            zoom: maxClusterZoomLevel + 1
        });
    }, function (error) {
        //If an error occurs when trying to access the users position information, display an error message.
        switch (error.code) {
            case error.PERMISSION_DENIED:
                alert('User denied the request for Geolocation.');
                break;
            case error.POSITION_UNAVAILABLE:
                alert('Position information is unavailable.');
                break;
            case error.TIMEOUT:
                alert('The request to get user position timed out.');
                break;
            case error.UNKNOWN_ERROR:
                alert('An unknown error occurred.');
                break;
        }
    });
}

代码示例由以下人员提供:https://github.com/Azure-Samples/AzureMapsCodeSamples/issues/57

© www.soinside.com 2019 - 2024. All rights reserved.