【问题标题】:Extracting Address Types from Google Geocdoe从 Google 地理编码中提取地址类型
【发布时间】:2012-05-02 09:10:43
【问题描述】:

目标:尝试对用户输入的地址进行地理编码,然后获取信息,将其放入表单并将此信息输入数据库。

到目前为止的路:我已经用下面的代码成功地完成了这个任务:

$(function() {
    $("#address").autocomplete({
        //This bit uses the geocoder to fetch address values
        source : function(request, response) {
            geocoder.geocode({
                'address' : request.term
            }, function(results, status) {
                response($.map(results, function(item) {
                    return {
                        label : item.formatted_address,
                        value : item.formatted_address,
                        latitude : item.geometry.location.lat(),
                        longitude : item.geometry.location.lng(),
                        streetNo : item.address_components[0].long_name,
                        streetName : item.address_components[1].long_name,
                        town : item.address_components[2].long_name,
                        province : item.address_components[4].long_name,
                        postal : item.address_components[6].long_name,
                        country : item.address_components[5].long_name
                    }

                }));
            })
        },
        //This bit is executed upon selection of an address
        select : function(event, ui) {

            //Now that an address has been selected, show the address form
            $("#newReview").show('fast');
            $("#latitude").val(ui.item.latitude);
            $("#longitude").val(ui.item.longitude);
            $("#streetNo").val(ui.item.streetNo);
            $("#streetName").val(ui.item.streetName);
            $("#streetNo").val(ui.item.streetNo);
            $("#town").val(ui.item.town);
            $("#postal").val(ui.item.postal);
            $("#province").val(ui.item.province);
            var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
            marker.setPosition(location);
            map.setCenter(location);

            $(".correctAddress").click(function() {
                if($(".correctAddress").val() == "yes") {
                    $("#map").hide('fast');
                };
            });

            $(".inCorrectAddress").click(function() {
                if($(".inCorrectAddress").val() == "no") {
                    $("#map").show('fast');
                };
            });
        }
    });
});

注意我只包含了执行“工作”的代码。

现在做什么::用户可以查找地址,他们会收到自动完成建议,当他们选择地址时,会出现一个包含一些字段的“表单”,其中包含地址信息已经有人了。

但问题是,根据最终选择的地址,信息可能并不总是匹配。

I.E. 1139 Ontario Rd, Welland ON, L3B 5E5, Canada 可以正常工作,并且字段将正确填充,但如果用户选择“地点”或不正确的地址,例如:

Cornelia Ct、81 Cornelia St W、Smiths Falls ON 等......

然后信息不好,因为我得到了“Cornelia Ct”这个名字,而不是街道号码,应该是 81,因为在这种情况下:

streetNo : item.address_components[0].long_name,

那个数组节点其实就是“Cornelia Ct”这个名字,就是地址的第一部分。

我需要做的是提取地理编码返回的“类型”,如下例所示:https://developers.google.com/maps/documentation/geocoding/#JSON 或此链接上的信息: https://developers.google.com/maps/documentation/geocoding/#Types

这样,如果我能做到这一点,街道编号将始终是“street_number”和街道名称“route”等......等等......,让我的数据保持有效和统一。

我已经在 stackoverflow 上查看了这个问题:

How to return address types from google maps geocode? 并且该答案有效,但它正在做同样的事情。

诚然,我几乎没有解析 JSON 的经验,但就我所理解的和迄今为止所尝试的而言,我认为有一种方法可以使用实际的“类型”名称而不是数组的节点号?

我尝试过各种方法,例如:

item.address_components['street_name'].long_name

以及它的变体,但我似乎无法破解它。

此代码的第二个问题是,通过询问这些特定结果,自动完成搜索只会在您已经输入大部分内容时为您提供建议,就好像它在猜测之前等待匹配所有这些字段一样。

如果我可以在地理编码的“返回”之外使用数组,理论上它应该允许更流畅的搜索,就像我删除时一样:

streetNo : item.address_components[0].long_name,
streetName : item.address_components[1].long_name,
town : item.address_components[2].long_name,
province : item.address_components[4].long_name,
postal : item.address_components[6].long_name,

提前感谢各位抽出宝贵时间!

【问题讨论】:

  • 我的问题是不是发错地方了?到目前为止,我自己没有取得任何进展,它可能就在我的脸上,但到目前为止我无法确定它。
  • 您的问题中包含的信息太多,而且涉及的方向也很多,很难弄清楚您到底在问什么。您要解决的具体问题是什么?
  • 而不是通过它的编号引用数组,即 item.address_components[4].long_name。我需要通过它的“类型”来引用数组,即:item.address_components['street_name'].long_name 使用'street_name'而不是'2',但每次我尝试时,代码都不会运行。我需要类型而不是数字的原因是地址并非都以统一的结构返回,具体取决于用户选择的内容。 IE。有时它只是一个城镇和州,有时是一个完整的 5 部分地址(街道编号、街道名称等......)。

标签: jquery json google-geocoder google-geocoding-api


【解决方案1】:

我自己也解决了这个问题,这是我的解决方案。我在输入中使用了livequery,因为它可以在我的代码中隐藏/显示,但是如果您愿意,可以编辑此行以删除插件。

$("#GeneralLocation").livequery(function() {
  $(this).autocomplete({
    //This bit uses the geocoder to fetch address values
    source: function(request, response) {
      geocoder.geocode( {'address': request.term }, function(results, status) {
        response($.map(results, function(item) {
          return {
            label:  item.formatted_address,
            value: item.formatted_address,
            address_components: item.address_components,
            latitude: item.geometry.location.lat(),
            longitude: item.geometry.location.lng()
          }
        }));
      })
    },
    minLength: 3,  //only start suggestions after this many chars
    //Executed upon selection of an address
    select: function(event, ui) {

  // Inline function, Return an address field by type
  function getaddrfield(addr,type) {
    var ret = "";
    $.each(addr,function(index, c) {
      if ($.inArray(type,c.types)>=0)
        ret = c.long_name;
    });
    return(ret);
  }

      loc_suburb = getaddrfield(ui.item.address_components, "locality");
      loc_state = getaddrfield(ui.item.address_components, "administrative_area_level_1");
      loc_country = getaddrfield(ui.item.address_components, "country");
    }
});

非常欢迎提示,但我希望对某人有所帮助。

唐 祝你有美好的一天

【讨论】:

    猜你喜欢
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2020-01-16
    • 2013-10-05
    相关资源
    最近更新 更多