[英]How do I populate two form fields - one with coordinates, one with address - using the HERE API?
我正在嘗試實現的是一個按鈕,單擊該按鈕可返回用戶最接近的地址,並使用此數據填充表單字段。 到目前為止,我已經能夠使用HTML Geolocation API並將用戶的當前坐標返回到表單字段。 因此,我想在此基礎上添加的是將這些坐標反向地理編碼到街道地址並返回到不同的表單字段。 我正在嘗試使用HERE API來實現這個問題的反向地理編碼部分,但是 - 正如我所說的 - 我對JS感到厭煩並且不知道代碼的外觀。
我返回用戶坐標的代碼在這里工作正常:
window.onload = function() {
var currentLocation = document.getElementById('coordinatesStore');
document.querySelector('.add-button').addEventListener('click', () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(({
coords: {
latitude,
longitude
}
}) => {
currentLocation.value = latitude + ", " + longitude;
});
} else {
currentLocation.value = "Geolocation is not supported by this browser.";
}
});
}
API 文檔中的示例的反向地理編碼坐標代碼如下,但我無法弄清楚如何集成這兩個:
var platform = new H.service.Platform({
app_id: 'blahblahblah',
app_code: 'rararararararr'
});
function reverseGeocode(platform) {
var geocoder = platform.getGeocodingService(),
parameters = {
prox: latitude + ", " + longitude,
mode: 'retrieveAddresses',
maxresults: '1'};
geocoder.reverseGeocode(parameters,
function (result) {
alert(result);
}, function (error) {
alert(error);
});
}
以下是一個例子。 如果您有任何疑問,請告訴我。
async function getLongLat() { //adding promise just for safe side to get it resolve first return new Promise((resolve, reject) => { let longitude = 'a', latitude = 'b' resolve( { longitude, latitude }); }); } async function reverseGeocode(platform) { //var geocoder = platform.getGeocodingService(), try { const {latitude, longitude} = await getLongLat(); console.log(latitude, longitude); }catch(err){ //here error is getLongLat reject for any XYZ reason } } reverseGeocode()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.