javascript - jQuery Locationpicker 设置多个位置

标签 javascript jquery google-maps location

我昨天遇到了一个问题 location picker已解决here

这适用于三分之二的情况,但对于最后一种情况,我不知道如何让它工作。

总结我之前的问题:我有一个位置选择器,我需要在初始化后从中提取某些值。为了实现这一点,我使用了这段代码,函数 setLocation 是我自己的并且按预期工作。

$("#locationpicker").locationpicker({
    location: {latitude: 10, longitude: 10},
    radius: 10,
    inputBinding:
    {
        latitudeInput: $("#locationpicker_lat"),
        longitudeInput: $("#locationpicker_lon"),
        radiusInput: $("#locationpicker_radius"),
        locationNameInput: $("#locationpicker_address")
    },
    oninitialized: function(component){
        setLocation("end");

        //1. attempt
        component.location = {latitude: 20, longitude: 20};

        //2. attempt
        var mapContext = component.locationpicker("map");
        mapContext.map.radius = 500;

        //3. attempt - I work, but then setLocation below not any more
        component.locationpicker("location", {latitude:10.0, longitude:10.0});

        setLocation("start");
    },
    enableAutocomplete: true
});

我调用函数 setLocation() 两次,但需要更改它们之间的位置。我在位置选择器的文档中找不到如何设置新位置(纬度、经度、半径),并且各种尝试也失败了。其中两个可以在代码中查看。

我考虑过使用原来的google maps API但不知道这是否有效以及到底如何使用它。

我希望我足够详细地描述了我的问题,如果缺少任何信息,我会立即提供。

更新1

我能够将位置更改为我需要的值,但第二组变量的提取不再正确。

这在 oninitilaized 函数中起作用

component.locationpicker("location", {latitude:10.0, longitude:10.0});

由于函数 setLocation 在当前状态下不起作用,我也将其发布在这里:

function setLocation(option)
{
    var lat = $("#locationpicker_lat").val();
    var lon = $("#locationpicker_lon").val();
    var add = $("#locationpicker_address").val();
    var rad = $("#locationpicker_radius").val();

    if(rad == "")
    {
        rad = 0;
    }

    if(option == "start")
    {
        document.getElementById("start_location_lat").value = lat;
        document.getElementById("start_location_lon").value = lon;
        document.getElementById("start_location_rad").value = rad;

        document.getElementById("display_start_location").innerHTML = add + ", Radius: " + rad + "m";
    }
    else
    {
        document.getElementById("end_location_lat").value = lat;
        document.getElementById("end_location_lon").value = lon;
        document.getElementById("end_location_rad").value = rad;

        document.getElementById("display_end_location").innerHTML = add + ", Radius: " + rad + "m";
    }
}

最佳答案

尝试为开始和结束位置创建单独的监听器。这样,您可以更轻松地调试应用程序并使其更易于理解。

示例代码

<div id="start" style="width: 500px; height: 400px;"></div>
<div id="end" style="width: 500px; height: 400px;"></div>

<script>

function setLocation_start(){
//your code for start
$('#start').locationpicker();
}

function setLocation_end(){
//your code for end
$('#end').locationpicker();
}

</script>

该图像是开始和结束位置的单独监听器的示例。

enter image description here

关于javascript - jQuery Locationpicker 设置多个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377419/

相关文章:

javascript - 拆分数组中包含的字符串并保存到另一个数组中

java - Android 谷歌地图问题

javascript - 根据 id 切换谷歌地图标记

javascript - 正则表达式返回错误值

JavaScript。计算百分比结果有 1% 的差异

javascript - 解析 JSON 给出 "unexpected token o"错误

javascript - 隐藏的文本区域填充在 IE 11 中不起作用

javascript - 是否仍然需要限制 $(window).scroll()?

javascript - 迭代列表并显示在下拉列表中

javascript - 如何在 Google Place AutoComplete API 上添加 session token