【问题标题】:Trying to get Leaflet Map to change map view to selected country option试图让 Leaflet Map 将地图视图更改为选定的国家/地区选项
【发布时间】:2021-10-17 21:23:46
【问题描述】:

我有一个<select> 列表,其中包含一堆国家/地区名称。选择一个选项时,value 987654322 <option> 987654323 @元素的属性(ISO)用于PHP CURL例程以通过JSON文件搜索以返回国家资本的Latlng。我希望这个 latlng 信息然后相应地调整地图,换句话说,将视图设置为所选国家/地区。

getPHP.js:

$('#countrySearch').click(function() {
    console.log("Button clicked");
    $.ajax({
        url: "libs/php/getCountryISO.php",
        type: 'POST',
        dataType: 'json',
        data: {
            iso: $('#countrySelect option:selected').val()
        },
        success: function(result) {

            console.log(JSON.stringify(result));
            console.log("JSON stringified");

            if (result.status.name == "ok") {

                console.log("All recieved well.");
                $(mymap).setView(result['data']['latlng']);

            }
        
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(jqXHR);
        }
    })
});

script.js:

//Loading Map
var mymap = L.map('mapid').locate({setView: true, maxZoom: 16}); //Set Location to Users Location
      L.tileLayer('https://tile.jawg.io/jawg-light/{z}/{x}/{y}{r}.png?access-token=nr1yo2LitcA3V1WiGnpQNQTE2ooNfxu7xxHlxldA7OirWsWMyBSLgoSzyVksXpdG', {}).addTo(mymap);
      mymap.attributionControl.addAttribution("<a href=\"https://www.jawg.io\" target=\"_blank\">&copy; Jawg</a> - <a href=\"https://www.openstreetmap.org\" target=\"_blank\">&copy; OpenStreetMap</a>&nbsp;contributors");
console.log("Leaflet Map Loaded.")

我尝试过使用 jQuery 选择器来调整不同的东西,例如 'mapid',但目前我遇到了以下问题:

Uncaught TypeError: $(...).setView is not a function
    at Object.success (getPHP.js:19)
    at c (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at l (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)

【问题讨论】:

    标签: javascript jquery curl


    【解决方案1】:

    设法弄明白了。

    在我的 ajax 调用中,我改变了:

    if (result.status.name == "ok") {
    
                console.log("All recieved well.");
                $(mymap).setView(result['data']['latlng']);
    
    }
    

    if (result.status.name == "ok") {
    
                console.log("All recieved well.");
                mymap.setView(result['data']);
    
     }
    

    不知道为什么我首先要使用 jQuery 选择器,但是我想你学会了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2016-01-11
      • 2023-01-20
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多