[英]How to pass longitude and latitude within typeahead script
我有一個在預輸入腳本中創建的值,該值在我嘗試將地理坐標與預輸入項一起傳遞之前可以正常工作。 如果我對geo變量進行硬編碼,則可以正常工作。 我將下面的兩個VAR示例細分為哪些有效,哪些無效。 有任何想法嗎? 請幫助並謝謝
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("lat").value = position.coords.latitude;
document.getElementById("lon").value = position.coords.longitude;
如果我將下面兩行代碼這樣的值硬編碼,則一切正常。 只是測試值
var lat = 123.123444;
var lon = 100.999999;
但是,如果我接下來的兩行,我的typeahead腳本將顯示下拉結果,但不會讓我選擇結果
var lat = position.coords.latitude;
var lon = position.coords.longitude;
}
$(document).ready(function() {
$('#my_input').typeahead({
source: function(query, result) {
$.ajax({
url: "search_script.php",
method: "POST",
data: {
query: query
},
dataType: "json",
success: function(data) {
result($.map(data, function(item) {
return item;
}));
}
})
},
updater: function(item) {
location.href = 'cart.php?shop_name=' + item + '&latitude=' + lat + '&longitude=' + lon
return item
}
});
});
</script>
將showPosition函數更改為以下內容。 我還將變量ID long更改為getlon,只要它是保留字,也將lat更改為getlat。 3天的毅力
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
lon=document.getElementById("getlon").value;
lat=document.getElementById("getlat").value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.