麻辣GIS微信平台

更多 GIS 干货

微信关注不错过

利用jquery解析Google Map的geocode地址数据

在调用Google地图的geocode服务时,会得到一组json数据或者xml数据,但是并不适合直接显示在网页上,所以需要对返回的结果做进一步的解析。这里提供利用jquery解析json的方法。

返回的数据格式如下:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Kishim",
               "short_name" : "Kishim",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "巴达赫尚",
               "short_name" : "巴达赫尚",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "阿富汗",
               "short_name" : "AF",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Kishim, 阿富汗",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.0190016,
                  "lng" : 70.63659679999999
               },
               "southwest" : {
                  "lat" : 36.265314,
                  "lng" : 70.10925279999999
               }
            },
            "location" : {
               "lat" : 36.5712949,
               "lng" : 70.3360691
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.0190016,
                  "lng" : 70.63659679999999
               },
               "southwest" : {
                  "lat" : 36.265314,
                  "lng" : 70.10925279999999
               }
            }
         },
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "巴达赫尚",
               "short_name" : "巴达赫尚",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "阿富汗",
               "short_name" : "AF",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "阿富汗巴达赫尚",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.490611,
                  "lng" : 74.89019499999999
               },
               "southwest" : {
                  "lat" : 35.44512100000001,
                  "lng" : 69.986097
               }
            },
            "location" : {
               "lat" : 36.7347725,
               "lng" : 70.81199529999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.490611,
                  "lng" : 74.89019499999999
               },
               "southwest" : {
                  "lat" : 35.44512100000001,
                  "lng" : 69.986097
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "阿富汗",
               "short_name" : "AF",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "阿富汗",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.49087670000001,
                  "lng" : 74.88986199999999
               },
               "southwest" : {
                  "lat" : 29.3772,
                  "lng" : 60.5170005
               }
            },
            "location" : {
               "lat" : 33.93911,
               "lng" : 67.709953
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.49087670000001,
                  "lng" : 74.88986199999999
               },
               "southwest" : {
                  "lat" : 29.3772,
                  "lng" : 60.5170005
               }
            }
         },
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

可以看出,这个数据是一个多重嵌套的json。我们需要提取results下的formatted_address。解析方法如下:

$.ajax({
        url:'http://maps.google.com/maps/api/geocode/json?latlng=36.910093,70.403945&language=zh-CN&sensor=false,
        type:'get',
        dataType:'json',
        success:function(resp){
            if(resp['status']==='OK')
            {
                alert(resp.results[2].formatted_address);
            }
        }   
    });

PS:在使用这段代码之前,需要引入jquery库。

<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.8.2/jquery.min.js"></script>

相关阅读

麻辣GIS-Sailor

作者:

GIS爱好者,学GIS,更爱玩GIS。

声明

1.本文所分享的所有需要用户下载使用的内容(包括但不限于软件、数据、图片)来自于网络或者麻辣GIS粉丝自行分享,版权归该下载资源的合法拥有者所有,如有侵权请第一时间联系本站删除。

2.下载内容仅限个人学习使用,请切勿用作商用等其他用途,否则后果自负。

手机阅读
公众号关注
知识星球
手机阅读
麻辣GIS微信公众号关注
最新GIS干货
关注麻辣GIS知识星球
私享圈子

留言板(小编看到第一时间回复)