谷歌地图获得地区标题

时间:2017-11-23 14:20:32

标签: google-maps

enter image description here

我无法从Google地图地理编码器中提取自然区域的标题(在地形模式下)。我只能从结果[0](红色箭头)显示最近的道路名称。有没有办法提取地图上显示的最近的标题(绿箭头)?是否可以在结果数组中使用?

var latlng = {lat: parseFloat(lat), lng: parseFloat(lng)};    
var geocoder = new google. maps. Geocoder;
geocoder. geocode({'location': latlng}، function(results، status) {
if (status === 'OK') {
   if (results[0]) {
   $("#place"). val(results[0]. formatted_address);
   } else {
   //No title found
   }
   } else {
   //window. alert('Geocoder failed due to: ' + status);
  }
});

1 个答案:

答案 0 :(得分:1)

这是对Google地图结果的回复,在address_components中您拥有地区或地区的标题  我想你想要这个results[0].address_components[2]

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}