javascript – 从城市名称获取邮政编码的最简单方法

前端之家收集整理的这篇文章主要介绍了javascript – 从城市名称获取邮政编码的最简单方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想搞乱我的任务并按城市名称而不是邮政编码搜索天气(我现在如何设置).使用城市名称输入字符串并从中获取邮政编码最简单的方法是什么?非常感谢帮助!谢谢!

解决方法

谷歌可以帮助你!

https://developers.google.com/maps/documentation/geocoding/

该zip实际上被Google称为“postal_code”.

"long_name": "94043","short_name": "94043","types": postal_code

例如,假设您想获得Clarkston,MI的拉链……

http://maps.googleapis.com/maps/api/geocode/json?address=Clarkston+MI&sensor=true

返回:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Clarkston","short_name" : "Clarkston","types" : [ "locality","political" ]
            },{
               "long_name" : "Oakland","short_name" : "Oakland","types" : [ "administrative_area_level_2",{
               "long_name" : "Michigan","short_name" : "MI","types" : [ "administrative_area_level_1",{
               "long_name" : "United States","short_name" : "US","types" : [ "country",{
               "long_name" : "48346","short_name" : "48346","types" : [ "postal_code" ]
            }
         ],"formatted_address" : "Clarkston,MI 48346,USA","geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 42.7418310,"lng" : -83.41402409999999
               },"southwest" : {
                  "lat" : 42.7252370,"lng" : -83.42880730000002
               }
            },"location" : {
               "lat" : 42.73511960,"lng" : -83.41929410
            },"location_type" : "APPROXIMATE","viewport" : {
               "northeast" : {
                  "lat" : 42.74331460,"lng" : -83.40328670
               },"southwest" : {
                  "lat" : 42.72692350,"lng" : -83.43530149999999
               }
            }
         },"political" ]
      }
   ],"status" : "OK"
}

编辑

如果您没有收到第一次通话的邮政编码,则必须使用第一次通话的坐标再次拨打同一个网络服务.还是很简单 – 对史蒂文斯角,威斯康星州的呼吁如下:

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.52357920000001,-89.5745630&sensor=true

您可以从“位置”获取lat / lng值.希望这可以帮助!

原文链接:https://www.f2er.com/js/153849.html

猜你在找的JavaScript相关文章