Ajax的传单setView(城市名称或邮政编码)

问题描述 投票:0回答:1

我们正在尝试从gmaps切换到Leaflet。设置地图很好,我们所有商店的标记也都可以工作。我为这个用于搜索城市的内置解决方案使用了传单搜索。

但是它正在对keyPress或类似的东西起作用。

我们希望输入一个带有单独按钮的提交。

[之前,我们只是使用了一个带有来自gmaps的特定URL的ajax,并附加了searchvalue。但是用Leaflet我无法做到...它看起来像这样:

function geocode(key)
{
var address = key.replace(/ /g, '+');
var url = 'https://maps.googleapis.com/maps/api/geocode/json';
url += "?key=" + SitePreferences.GOOGLE_MAP_API_KEY;

$.ajax({
    url     : url,
    data    : {address:address},
    dataType: "json",
    success : function (json)
    {
        setGeocode(json);
    },
    error: function()
    {
        console.log("Google map API geocode error");
    }
});}

我该如何使用传单?我尝试了某事,但是没有成功:

var url = 'https://nominatim.openstreetmap.org/search?format=json&q={address}';

        $.ajax(
        {
             url:url, 
             dataType: "json",
             success: function(data)
             {
                 console.log(data),
             }
        });
javascript jquery ajax leaflet
1个回答
0
投票

您的代码很好,它有语法错误,我也使用数据而不是直接从url发送了参数

$('#find').click(function(){
  var url = 'https://nominatim.openstreetmap.org/search';
  $.ajax(
  {
    url: url, 
    dataType: "json",
    data:{
      format:'json',
      q: '{' + $("#address").val() + '}'
    },
    success: function(data)
    {
       console.log(data);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="address" />
<button id="find">find address</button>
© www.soinside.com 2019 - 2024. All rights reserved.