【问题标题】:JavaScript Google Maps Geolocation How can I get the Zipcode, City, State from this?JavaScript Google Maps Geolocation 我如何从中获取邮政编码、城市、州?
【发布时间】:2012-07-08 10:20:51
【问题描述】:

好吧,我是从我找到的一个例子中提取的,但是我的大脑被热炸了,无论如何 google api 总是对我做一些事情

function codeLatLng(lat, lng)
{
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status)
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
            //console.log(results)
            if(results[1])
            {
                //formatted address
                //alert(results[0].formatted_address)
                //find country name
                $('#geolocation_latlng').html(latlng);
                for(var i=0; i<results[0].address_components.length; i++)
                {
                    for(var b=0;b<results[0].address_components[i].types.length;b++)
                    {

                        //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                        if(results[0].address_components[i].types[b] == "administrative_area_level_1")
                        {
                            //this is the object you are looking for
                            city=results[0].address_components[i];
                            break;
                        }
                    }
                }
            //city data
            //alert(city.short_name + " " + city.long_name)
            }
            else
            {
                alert("Could not Determin Location");
            }
      }
      else
      {
          alert("Location dection failed: " + status);
      }
    });
}

可以安全地假设 lat 和 lon 已经通过并且我得到了结果,我注释掉了一个关于 formated_address 的警报,我想要做的是弄清楚我可以如何获取邮政编码、州、城市该格式化地址并将其显示在屏幕上。但就像我说的,我现在炸了,我在敲钟,所以我需要一些帮助来启动我。

【问题讨论】:

  • 您与哪些国家/地区合作?
  • 现在只是美国,虽然让它足够开放以便将来扩展到其他国家会很好,因为我确实打算让其他国家迟早访问该网站

标签: javascript jquery google-maps geolocation


【解决方案1】:

My demo 是一个滑稽的大括号集合,但我们的想法是查看results.types 的内部:

 "locality"                     // CITY
 "administrative_area_level_1"  // STATE
 "postal_code"                  // ZIP

在迭代过程中,当您匹配这些类型时,请填写值。我询问了国家,因为这种方法在文档中解释为与美国相对应,但我不知道它在新加坡是否有效。正如他们所说,这不是一门精确的科学。

因此,如果您进行扩展,您可能必须先检查国家/地区,然后匹配另一个与 Google 地图类型对应的 wantedTypes 对象(在另一个国家/地区,该州可能是 Admin. area level 2)。

我使用简称是因为您很可能想要 AZ 而不是 Arizona。我没有去检查城市是否不同,例如圣保罗(短)与圣保罗。

    if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {
            var typeCorrespondence = {
                "city": "locality",
                "state": "administrative_area_level_1",
                "zipcode": "postal_code"
            };

            var geocoderResults = {};

            var components = results[0].address_components;

            for (var i = 0; i < components.length; i++) {
                for (var j = 0; j < components[i].types.length; j++) {

                    for (myType in typeCorrespondence) {
                        if (typeCorrespondence[myType] == components[i].types[j]) {
                            geocoderResults[myType] = components[i].short_name;
                        }
                    }
                }
            }
            $("#result").val(JSON.stringify(geocoderResults));
        }
    }

【讨论】:

  • 看,这对我来说更有意义,是否有一个“傻瓜”版本的文档,我可以从低处开始并着手解决这个问题,因为我最大的问题是理解返回的对象。无论如何,谢谢你,这对我很有帮助,在你的建议中,我应该尝试将这个逻辑应用到我的原始帖子中,还是应该将你在这里的内容作为我的新起点?
  • 请在此处查看此更新代码jsfiddle.net/yV6xv/134 它更长但更清晰,更易于理解。最终生成的对象具有“城市、州、邮编”属性 :)(我将在一秒钟内更新答案)。我的回答和你的帖子结构大致相同,区别始于if(results[1]) 块。我使用results[0],但这并不重要,因为地理编码器通常会在该数组中返回 7 或 8。由于您有错误通知而我没有,因此您应该复制我在该 if(results[0]) 块中的内容。我认为文档写得很好,内部很难掌握。
猜你喜欢
  • 2013-04-05
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
相关资源
最近更新 更多