【问题标题】:JQuery adress autocomplete with geocodeapi.io使用 geocodeapi.io 的 JQuery 地址自动完成
【发布时间】:2021-02-15 00:49:14
【问题描述】:

我正在尝试使用 Jquery 为地址自动完成构建一个自动完成输入字段。 我选择 geocodeapi 作为来源,它是免费的,而且我没有 google api 的信用卡。

这是文档https://geocodeapi.io/documentation#autocomplete 自动完成工作,请求和回答来了,但我不知道如何格式化或读取 json 以便它在自动完成中显示地址,我变得未定义

$(function() {
    $("#customerAdress").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "https://app.geocodeapi.io/api/v1/autocomplete?apikey=mykey",
                dataType: "json",
                data: {
                    text: request.term
                },
                success: function(data) {
                    console.log("here comes data for test")
                    console.log(data)
                    var data = $.map(data, function(obj) {
                        return {
                            label: obj.Street+','+obj.Housnumber+','+ obj.zip+','+ obj.city, //here is the problem i think
                        };
                    });
                    response(data);
                }
            });
        },
        minLength: 1,
    });
});

最后,为什么可以使用层函数将结果还原到特定状态

【问题讨论】:

    标签: javascript jquery json autocomplete


    【解决方案1】:

    data 是一个对象。你必须映射data.features,而不是data。然后您将获得的每个对象将是:

     {
             "type":"Feature",
             "geometry":{
                "type":"Point",
                "coordinates":[
                   -73.976942,
                   40.760478
                ]
             },
             "properties":{
                "id":"way/118476502",
                "gid":"openstreetmap:address:way/118476502",
                "layer":"address",
                "source":"openstreetmap",
                "source_id":"way/118476502",
                "name":"666 Fifth Avenue",
                "housenumber":"666",
                "street":"Fifth Avenue",
                "accuracy":"point",
                "country":"United States",
                "country_gid":"whosonfirst:country:85633793",
                "country_a":"USA",
                "region":"New York",
                "region_gid":"whosonfirst:region:85688543",
                "region_a":"NY",
                "county":"New York County",
                "county_gid":"whosonfirst:county:102081863",
                "county_a":"NE",
                "locality":"New York",
                "locality_gid":"whosonfirst:locality:85977539",
                "locality_a":"NYC",
                "borough":"Manhattan",
                "borough_gid":"whosonfirst:borough:421205771",
                "neighbourhood":"Midtown West",
                "neighbourhood_gid":"whosonfirst:neighbourhood:85882233",
                "continent":"North America",
                "continent_gid":"whosonfirst:continent:102191575",
                "label":"666 Fifth Avenue, Manhattan, New York, NY, USA",
                "addendum":{
                   "osm":{
                      "wikidata":"Q2818016",
                      "wikipedia":"en:666 Fifth Avenue"
                   }
                }
             }
          },
    

    所以你想要obj.properties.streetobj.properties.country 等。

    const objs = data.features.map( obj => ({
          label: obj.properties.street+','+obj.properties.Housnumber+','+ obj.properties.zip+','+ obj.properties.city
    }));
    response(objs);
    

    【讨论】:

    • 谢谢,但是结果很奇怪,貌似是这个s12.directupload.net/images/201102/fyhkbid5.png
    • 啊,我没有它的工作,简单的标签是 obj.properties.label。非常感谢!
    • 我有一个问题,为什么我可以将搜索结果限制在像德国这样的国家,谢谢,我会这样做。
    猜你喜欢
    • 1970-01-01
    • 2013-10-24
    • 2023-03-12
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多