繁体   English   中英

如何使用HERE API填充两个表单字段 - 一个带坐标,一个带地址?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM