在给定地址的情况下从google.maps获取建筑物的名称

时间:2015-02-09 21:06:53

标签: javascript jquery google-maps

我正在使用google maps javascript API自动填写用户输入地址数据的表单字段。这是使用here所述方法的变体来完成的。

当用户提交包含带有地址数据的字段的表单时,程序会将地址组件的值分配给该表单中的隐藏字段:

function get_address_components(place) {
   var componentForm = {
       street_number: 'short_name', route: 'long_name',  locality: 'long_name',
        administrative_area_level_1: 'short_name',  country: 'long_name',   postal_code: 'short_name'
   };

   // add address component data to hidden form fields
   for (var i = 0; i < place.address_components.length; i++) {
      var addressType = place.address_components[i].types[0];
      var val = place.address_components[i][componentForm[addressType]];
      $( '#' + addressType).val(val);
   }
 }

 //when form is submitted, the function is called like this
 get_address_components( autocomplete.getPlace() )

这种方法很好,但如果建筑物有名称,这些组件似乎都不包括建筑物的名称(即“特朗普大厦”,“温德姆酒店”)。

如何修改上面的代码,以便建筑物名称也是添加到相应隐藏表单字段的值之一?

2 个答案:

答案 0 :(得分:1)

没有简单的方法,因为谷歌自动完成API不会返回该信息,示例响应是这样的:

{
   "html_attributions" : [
      "Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
   ],
   "results" : [
      {
         "formatted_address" : "529 Kent Street, Sydney NSW, Australia",
         "geometry" : {
            "location" : {
               "lat" : -33.8750460,
               "lng" : 151.2052720
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "827f1ac561d72ec25897df088199315f7cbbc8ed",
         "name" : "Tetsuya's",
         "rating" : 4.30,
         "reference" : "CnRmAAAAmmm3dlSVT3E7rIvwQ0lHBA4sayvxWEc4nZaXSSjRtfKRGoYnfr3d5AvQGk4e0u3oOErXsIJwtd3Wck1Onyw6pCzr8swW4E7dZ6wP4dV6AsXPvodwdVyqHgyGE_K8DqSp5McW_nFcci_-1jXb5Phv-RIQTzv5BjIGS0ufgTslfC6dqBoU7tw8NKUDHg28bPJlL0vGVWVgbTg",
         "types" : [ "restaurant", "food", "establishment" ]
      },
      {
         "formatted_address" : "Upper Level, Overseas Passenger Terminal/5 Hickson Road, The Rocks NSW, Australia",
         "geometry" : {
            "location" : {
               "lat" : -33.8583790,
               "lng" : 151.2100270
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
         "id" : "f181b872b9bc680c8966df3e5770ae9839115440",
         "name" : "Quay",
         "rating" : 4.10,
         "reference" : "CnRiAAAADmPDOkn3znv_fX78Ma6X5_t7caEGNdSWnpwMIdDNZkLpVKPnQJXP1ghlySO-ixqs28UtDmJaOlCHn18pxpj7UQjRzR4Kmye6Gijoqoox9bpkaCAJatbJGZEIIUwRbTNIE_L2jGo5BDqiosqU2F5QdBIQbXKrvfQuo6rmu8285j7bDBoUrGrN4r6XQ-PVm260PFt5kwc3EfY",
         "types" : [ "cafe", "bar", "restaurant", "food", "establishment" ]
      },
      {
         "formatted_address" : "107 George Street, The Rocks NSW, Australia",
         "geometry" : {
            "location" : {
               "lat" : -33.8597750,
               "lng" : 151.2085920
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "7beacea28938ae42bcac04faf79a607bf84409e6",
         "name" : "Rockpool",
         "rating" : 4.0,
         "reference" : "CnRlAAAAVK4Ek78r9yHV56I-zbaTxo9YiroCbTlel-ZRj2i6yGAkLwNMm_flMhCl3j8ZHN-jJyG1TvKqBBnKQS2z4Tceu-1kZupZ1HSo5JWRBKd7qt2vKgT8VauiEBQL-zJiKVzSy5rFfilKDLSiLusmdi88ThIQqqj6hKHn5awdj6C4f59ifRoUg67KlbpuGuuW7S1tAH_EyBl6KE4",
         "types" : [ "restaurant", "food", "establishment" ]
      },
      {
         "formatted_address" : "483 George Street, Sydney NSW, Australia",
         "events" : [
            {
              "event_id" : "7lH_gK1GphU",
              "summary" : "Google Maps Developer Meetup: Rockin' out with the Places API",
              "url" : "https://developers.google.com/places"
            }
          ],
         "geometry" : {
            "location" : {
               "lat" : -33.8731950,
               "lng" : 151.2063380
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/civic_building-71.png",
         "id" : "017049cb4e82412aaf0efbde890e82b7f2987c16",
         "name" : "Chinatown Sydney",
         "rating" : 4.0,
         "reference" : "CnRuAAAAsLNeRQtKD7TEUXWG6gYD7ByOVKjQE61GSyeGZrX-pOPVps2BaLBlH0zBHlrVU9DKhsuXra075loWmZUCbczKDPdCaP9FVJXB2NsZ1q7188pqRFik58S9Z1lcWjyVoVqvdUUt9bDMLqxVT4ENmolbgBIQ9Wy0sgDy0BgWyg5kfPMHCxoUOvmhfKC-lTefXGgnsRqEQwn8M0I",
         "types" : [
            "city_hall",
            "park",
            "restaurant",
            "doctor",
            "train_station",
            "local_government_office",
            "food",
            "health",
            "establishment"
         ]
      }
   ],
   "status" : "OK"
}

来自:https://developers.google.com/places/documentation/search?hl=en

答案 1 :(得分:1)

找到它:

$place = autocomplete.getPlace();
$building name = $place.name