![](/img/trans.png)
[英]how to get the current location of the user along with latitude and longitude in OpenLayers
[英]How to get current location (latitude and longitude) and send it to an reverse geocoding API?
我正在嘗試獲取當前的緯度和經度,並將其傳遞給名為OpenCage Geocoder API的Web API。 這是我所做的:
// reverse geocoding
function geocode() {
let lat,long;
window.onload = function() {
var startPos;
var geoSuccess = function(position) {
startPos = position;
lat = startPos.coords.latitude;
long = startPos.coords.longitude;
};
navigator.geolocation.getCurrentPosition(geoSuccess);
};
geoResponse = function(reponse)
{
document.getElementById("address").innerHTML = response.results[17].toString();
};
let script = document.createElement('script');
script.src = "https://api.opencagedata.com/geocode/v1/json?q="+ lat +","+ long +"&key=00f711f73483427c8577e646aa2bf4bf&jsonp=geoResponse";
document.body.appendChild(script);
}
我是編碼的新手,但我有點卡住了。 有沒有更簡單的方法來調用此特定的API,我很迷路。 任何幫助將非常感謝!
我已修改並附加了您的代碼段以供參考! 在您的getResponse中,您收到了作為響應的參數,並在代碼中調用了response.results [17] .toString(),該參數未定義!
window.onload = function() { navigator.geolocation.getCurrentPosition(function (position) { var lat = position.coords.latitude; var long = position.coords.longitude; var script = document.createElement('script'); script.src = "https://api.opencagedata.com/geocode/v1/json?q="+ lat +","+ long +"&key=00f711f73483427c8577e646aa2bf4bf&jsonp=geoResponse"; document.body.appendChild(script); }); } geoResponse = function(reponse){ document.getElementById("address").innerHTML = reponse.results[0].formatted; }
<div id="address"> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.