Google Places API-地址和电话号码?

时间:2014-11-17 04:15:08

标签: javascript jquery html google-maps google-maps-api-3

我正在尝试通过Google商家信息创建列表和地图。以下显示制造商和当地商店列表。现在它只显示商店的名称。我如何包含地址和电话?我会添加另一个对象规范吗?

更新:根据此链接:Google Places API - Places detail request undefined

应该添加

place.getDetails()。但是我的脚本现在只显示一个市场,而不是细节。发生了什么事?

更新更新:

HTML示例:http://simplecomputerblog.com/searchtest1.html

  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
    <script>
var map, placesList;
var infowindow;
var service; //declare globally


function initialize() {
  var pyrmont = new google.maps.LatLng(40.062664, -83.069101);

  map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: pyrmont,
    zoom: 15
  });

  var request = {
    location: pyrmont,
    radius: 500,
    types: ['store']
  };
  infowindow = new google.maps.InfoWindow();
   placesList = document.getElementById('places');
 service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
service.getDetails(request, callback);

}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
  map: map,
  position: place.geometry.location
 });

var request = { reference: place.place_id };
service.getDetails(request, function(details, status) {
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
    infowindow.open(map, this);
   });
 });
 placesList.innerHTML += '<li>' + place.name + '</li>';
}



google.maps.event.addDomListener(window, 'load', initialize);

    </script>

1 个答案:

答案 0 :(得分:2)

通过documentation,您可以轻松处理您获取的地点的numberaddress

  function createMarker(place) {
   var placeLoc = place.geometry.location;
   var marker = new google.maps.Marker({
     map: map,
     position: place.geometry.location
   });

   google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name+place.formatted_address+place.formatted_phone_number);
    infowindow.open(map, this);
   });
   placesList.innerHTML += '<li>' + place.name + '</li>';
 }

再次来自docs

  

InfoWindow的内容可能包含一串文本,一个片段   HTML或DOM元素。要设置内容,请指定它   在InfoWindowOptions中或在InfoWindow上调用setContent()   明确。

修改 - 正确指出nearbySearch formatted_address formatted_phone_number并未在result对象中返回getdetails function createMarker(place) { var placeLoc = place.geometry.location; var marker = new google.maps.Marker({ map: map, position: place.geometry.location }); var request = { placeId: place.place_id }; service.getDetails(request, function(details, status) { google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number); infowindow.open(map, this); }); }); placesList.innerHTML += '<li>' + place.name + '</li>'; } ,为此您必须制作另一个service请求,就像您已将其合并到您的代码中一样。它可能看起来像(未经测试):

var service; //declare globally
function intialize(){
   ...
   service = new google.maps.places.PlacesService(map); //initialize here

   ...
}

{{1}}可用于您使用它的函数的范围是很重要的。所以在所有函数之外声明它并正确地初始化它:

{{1}}

因此,您可以相应地格式化该内容字符串并填充信息窗口。希望它能让你开始朝着正确的方向前进。