Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Google places api中使用附近搜索查找地点详细信息?_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 如何在Google places api中使用附近搜索查找地点详细信息?

Javascript 如何在Google places api中使用附近搜索查找地点详细信息?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我无法在单击每个地点图标时显示地点详细信息。我阅读了文档,认为我的编码是正确的。有人有什么建议吗?它也没有给我所有我应该得到的结果。它只输出1家附近的酒店,而实际上大约有30家。谢谢 var map; var infowindow; function initMap() { var messiah = {lat: 40.158350, lng: -76.987454}; map = new google.maps.Map(document.getElementById

我无法在单击每个地点图标时显示地点详细信息。我阅读了文档,认为我的编码是正确的。有人有什么建议吗?它也没有给我所有我应该得到的结果。它只输出1家附近的酒店,而实际上大约有30家。谢谢

  var map;
  var infowindow;
  function initMap() {
    var messiah = {lat: 40.158350, lng: -76.987454};
    map = new google.maps.Map(document.getElementById('map'), {
      center: messiah,
      zoom: 12
    });

    var marker = new google.maps.Marker({
        position: messiah,
        map: map,
        title: 'Messiah College'
    });

    var request = {
        location: messiah,
        radius: 10000,
        type: ['lodging']
    }
    infowindow = new google.maps.InfoWindow();
    var 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,
        icon: {
            url: 'http://maps.gstatic.com/mapfiles/circle.png',
            anchor: new google.maps.Point(10, 10),
            scaledSize: new google.maps.Size(10, 17)
        },
        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);
  });
}); 
var映射;
var信息窗口;
函数initMap(){
var messiah={lat:40.158350,lng:-76.987454};
map=new google.maps.map(document.getElementById('map'){
中心:弥赛亚,
缩放:12
});
var marker=new google.maps.marker({
职位:弥赛亚,
地图:地图,
标题:“弥赛亚学院”
});
var请求={
地点:弥赛亚,
半径:10000,
类型:[“住宿”]
}
infowindow=new google.maps.infowindow();
var service=newgoogle.maps.places.PlacesService(地图);
服务.nearbySearch(请求、回调);
获取详细信息(请求、回调);
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
对于(var i=0;i”+details.formatted\u address+“
”+details.website+“
”+details.rating+“
”+details.formatted\u电话号码); 打开(地图,这个); }); });

}你犯了几点错误

  • 对于service.getDetails()方法,传递引用属性的place\u id,但正确的是place.reference,而不是place\u id

  • 不应在()中调用service.getDetails(){…}, 因为getDetails方法是异步的。 这意味着你在短时间内打了很多次电话。 (也就是说,如果您得到10个结果,您的代码将在1秒内调用getDetails()方法。) 你的代码将被谷歌拒绝,因为短时间内有太多的请求

    为了避免这种情况,应该在单击标记后调用getDetails()方法

  • :

    
    html,正文{
    身高:100%;
    保证金:0;
    填充:0;
    }
    #地图{
    宽度:100%;
    身高:100%;
    }
    var映射;
    var信息窗口;
    var服务;
    函数initMap(){
    var messiah={
    拉脱维亚:40.158350,
    液化天然气:-76.987454
    };
    map=new google.maps.map(document.getElementById('map'){
    中心:弥赛亚,
    缩放:12
    });
    var marker=new google.maps.marker({
    职位:弥赛亚,
    地图:地图,
    标题:“弥赛亚学院”
    });
    var请求={
    地点:弥赛亚,
    半径:10000,
    类型:[“住宿”]
    }
    infowindow=new google.maps.infowindow();
    服务=新的google.maps.places.PlacesService(地图);
    服务.nearbySearch(请求、回调);
    }
    函数回调(结果、状态){
    if(status==google.maps.places.PlacesServiceStatus.OK){
    结果:forEach(createMarker);
    }
    }
    函数createMarker(place){
    var placeLoc=place.geometry.location;
    var marker=new google.maps.marker({
    地图:地图,
    图标:{
    网址:'http://maps.gstatic.com/mapfiles/circle.png',
    主播:新谷歌地图点(10,10),
    scaledSize:新的google.maps.Size(10,17)
    },
    位置:place.geometry.location
    });
    marker.addListener('click',function()){
    var请求={
    参考:place.reference
    };
    service.getDetails(请求、函数(详细信息、状态){
    infowindow.setContent([
    details.name,
    details.u地址,
    详情.网址:,
    详情.评级,
    详细信息。格式化的电话号码。加入(“
    ”); 信息窗口。打开(地图、标记); }); }) } window.onload=initMap;
    您犯了几点错误

  • 对于service.getDetails()方法,传递引用属性的place\u id,但正确的是place.reference,而不是place\u id

  • 不应在()中调用service.getDetails(){…}, 因为getDetails方法是异步的。 这意味着你在短时间内打了很多次电话。 (也就是说,如果您得到10个结果,您的代码将在1秒内调用getDetails()方法。) 你的代码将被谷歌拒绝,因为短时间内有太多的请求

    为了避免这种情况,应该在单击标记后调用getDetails()方法

  • :

    
    html,正文{
    身高:100%;
    保证金:0;
    填充:0;
    }
    #地图{
    宽度:100%;
    身高:100%;
    }
    var映射;
    var信息窗口;
    var服务;
    函数initMap(){
    var messiah={
    拉脱维亚:40.158350,
    液化天然气:-76.987454
    };
    map=new google.maps.map(document.getElementById('map'){
    中心:弥赛亚,
    缩放:12