如何访问对象的属性

时间:2016-07-26 06:37:52

标签: javascript ajax google-maps

我对谷歌地图api有ajax请求,如下所示,

$.ajax({ 
  type: "POST",
  async: false,
  dataType: "json",
  url: 'https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyDWPSMsE_CuOAk5iKJ48ReJQYRe9NUn_mo&address='35010'&sensor=false',
  success: function (response) { 

  console.log(response);

  var latlong = JSON.stringify(response.results[0].geometry.filter(function(obj){ 
  return obj.types.indexOf("location") != -1 
  })[0].lat, null, 4);

  latlong = latlong.replace(/['"]+/g, '');

  alert(latlong);

  }
});  

我得到了JSON响应,

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "35010",
               "short_name" : "35010",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Alexander City",
               "short_name" : "Alexander City",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Alabama",
               "short_name" : "AL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Alexander City, AL 35010, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 33.1044549,
                  "lng" : -85.76817199999999
               },
               "southwest" : {
                  "lat" : 32.703111,
                  "lng" : -86.050735
               }
            },
            "location" : {
               "lat" : 32.94464350000001,
               "lng" : -85.91000889999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 33.1044549,
                  "lng" : -85.76817199999999
               },
               "southwest" : {
                  "lat" : 32.703111,
                  "lng" : -86.050735
               }
            }
         },
         "place_id" : "ChIJgSRw98ETjIgROEXAlUkHoR8",
         "postcode_localities" : [ "ALEX CITY", "Alexander City" ],
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

我希望在几何中得到lat long值(在" location"字段中)。但是当我运行代码时,我得到了#34; jquery.min.js:4 Uncaught TypeError: response.results[0].geometry.filter is not a function"

请帮忙。我是javascript的新手。

3 个答案:

答案 0 :(得分:0)

    $.ajax({ 
      type: "POST",
      async: false,
      dataType: "json",
      url: 'https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyDWPSMsE_CuOAk5iKJ48ReJQYRe9NUn_mo&address='35010'&sensor=false',
      success: function (response) { 

      console.log(response);
    if(response.results[0].geometry.location != null){
      var latlong = response.results[0].geometry.location.lat +','+response.results[0].geometry.location.lng

}

      alert(latlong);

      }
    });  

请检查它是否有效

答案 1 :(得分:0)

从您的来源获取lat和lng in location =>几何,试试



$.ajax({ 
  type: "POST",
  async: false,
  dataType: "json",
  url: 'https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyDWPSMsE_CuOAk5iKJ48ReJQYRe9NUn_mo&address=35010&sensor=false',
  success: function (response) { 
  console.log(response);

  //var latlong = JSON.stringify(response.results.geometry.filter(function(obj){ return obj.types.indexOf("location") != -1})[0].lat, null, 4);

  //latlong = latlong.replace(/['"]+/g, '');

  alert(response.results[0].geometry.location.lat);
  alert(response.results[0].geometry.location.lng);

  }
});  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您只需访问latlong

即可
var latlong = response.results[0].geometry.location.lat+","+response.results[0].geometry.location.lng;