【问题标题】:How to pass longitude and latitude within typeahead script如何在预先输入的脚本中传递经度和纬度
【发布时间】:2018-04-09 17:08:50
【问题描述】:

我有一个在预输入脚本中创建的值,它可以正常工作,直到我尝试将地理坐标与我的预输入项目一起传递。如果我对地理变量进行硬编码,它可以正常工作。我已经分解了下面的两个 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>

【问题讨论】:

  • 哇,还没有回复,但我仍在努力。比我想象的要艰难得多

标签: javascript ajax geolocation


【解决方案1】:

将 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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    相关资源
    最近更新 更多