如何使用google place API中的地点ID来检索地点详细信息

时间:2018-02-28 15:04:54

标签: javascript php google-maps

我正在使用Google地方API从API检索地点评论,以下是我用于从Google地方API检索数据的代码,它工作正常,但它正在处理地名,我希望它在地方搜索ID。我附上了js代码和屏幕截图,它的工作正常。

屏幕截图

enter image description here

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: 41.9030632,
      lng: 12.466275999999993
    },
    zoom: 13
  });

  var input = document.getElementById('pac-input');

  var autocomplete = new google.maps.places.Autocomplete(input);
  autocomplete.bindTo('bounds', map);

  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map
  });
  marker.addListener('click', function() {
    infowindow.open(map, marker);
  });

  autocomplete.addListener('place_changed', function() {
    infowindow.close();
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      return;
    }

    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);
    }

    // Set the position of the marker using the place ID and location.
    marker.setPlace({
      placeId: place.place_id,
      location: place.geometry.location
    });
    marker.setVisible(true);

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
      'Place ID: ' + place.place_id + '<br>' +
      place.formatted_address);
    infowindow.open(map, marker);

    var service = new google.maps.places.PlacesService(map);

    var details_container = document.getElementById('details');

    service.getDetails({
      placeId: place.place_id
    }, function(place, status) {
      details_container.innerHTML ='<p><strong>Place ID:</strong> <code>' + place.place_id + '</code></p>' +
        '<p><strong>Address:</strong> <code>' + place.formatted_address + '</code></p><h1>Reviews</h1>';

        for(var a=0;a<5;a++){
            details_container.innerHTML +='<h2 style="font-weight:300;color:red;">'+place.reviews[a].author_name+'</h2>';
            details_container.innerHTML +='<p>'+place.reviews[a].rating+' Stars</p>';
            details_container.innerHTML +='<p> Comment > '+place.reviews[a].text+'</p>';
        }
         console.log(place.place_id);

    });

  }); // end autocomplete addListener
}

0 个答案:

没有答案