Javascript Autofit google使用位置id映射多个标记

Javascript Autofit google使用位置id映射多个标记,javascript,php,google-maps,google-maps-api-3,Javascript,Php,Google Maps,Google Maps Api 3,我正在使用谷歌地点自动完成。我有PlaceID,并使用google maps API成功地显示了它。 但所有的标记都不可见。我必须放大它才能看到所有的标记。 通过缩放调整地图后,我可以看到所有的标记。 如何在屏幕上自动调整所有标记 我的代码: var placeid_json = <?php echo $placeids; ?>; var openedInfoWindow = null; var bounds = new google.maps.LatLngBounds(); f

我正在使用谷歌地点自动完成。我有PlaceID,并使用google maps API成功地显示了它。 但所有的标记都不可见。我必须放大它才能看到所有的标记。 通过缩放调整地图后,我可以看到所有的标记。 如何在屏幕上自动调整所有标记

我的代码:

var placeid_json = <?php echo $placeids; ?>;

var openedInfoWindow = null;
 var bounds = new google.maps.LatLngBounds();
function initialize() {
    var latitude = 21.1202644,
        longitude = 79.0418986,
        radius = 8000,

    center = new google.maps.LatLng(latitude, longitude),
    mapOptions = {
        center: center,
        zoom: 10,

        scrollwheel: false
    };

var map = new google.maps.Map(document.getElementById("map"), mapOptions);



   setMarkers(center, radius, map);
}

function setMarkers(center, radius, map) {

var json = placeid_json;
for (var i = 0, length = json.length; i < length; i++) {
    var data = json[i];
    createMarker(data, map);
    }
}
function createMarker(data, map) {
            var service = new google.maps.places.PlacesService(map);
        service.getDetails({
            placeId: data.placeid
        }, function (result, status) {
            if (status != google.maps.places.PlacesServiceStatus.OK) {
                alert(status);
                return;
            }
            var marker = new google.maps.Marker({
                map: map,
                place: {
                    placeId: data.placeid,
                    location: result.geometry.location
                }
                //position: result.geometry.location
            });
            infoBox(map, marker, data, result);
        });

}
function infoBox(map, marker, data, result) {
    var infoWindow = new google.maps.InfoWindow();

    google.maps.event.addListener(marker, "click", function (e) {

        infoWindow.setContent(data.content);
        infoWindow.open(map, marker);
    });

    (function (marker, data) {

    google.maps.event.addListener(marker, "click", function (e) {

        infoWindow.setContent(data.content+"<br>"+result.name);
        infoWindow.open(map, marker);
    });
})(marker, data);
}


google.maps.event.addDomListener(window, 'load', initialize);
var placeid_json=;
var openedinfown=null;
var bounds=new google.maps.LatLngBounds();
函数初始化(){
var纬度=21.1202644,
经度=79.0418986,
半径=8000,
中心=新的google.maps.LatLng(纬度、经度),
映射选项={
中心:中心,,
缩放:10,
滚轮:错误
};
var map=new google.maps.map(document.getElementById(“map”)、mapOptions);
设置标记(中心、半径、地图);
}
功能设置标记(中心、半径、地图){
var json=placeid_json;
for(var i=0,length=json.length;i”+result.name);
信息窗口。打开(地图、标记);
});
})(标记、数据);
}
google.maps.event.addDomListener(窗口“加载”,初始化);
创建一个空对象

对于每个标记,获取其位置

var position = marker.getPosition();
bounds.extend(position);
扩展边界以包括该位置

var position = marker.getPosition();
bounds.extend(position);
对所有标记执行此操作后,使贴图适合这些边界

map.fitBounds(bounds);
  • 映射
    变量设为全局变量(就像现有边界对象一样)
  • 扩展边界并在details服务的回调函数中调用
    map.fitBounds
  • 代码片段:

    var placeid\u json=[{
    “placeid”:“ChIJu6HrLMVWIkcRWHTA90kiueI”,
    “内容”:“1”
    }, {
    “placeid”:“ChIJnXBuJ34zGUcRvt9FTKrPeeM”,
    “内容”:“2”
    }, {
    “placeid”:“Chijiwunhqx7pecrdjyqzrwyjs”,
    “内容”:“3”
    }];
    var openedinfown=null;
    var bounds=new google.maps.LatLngBounds();
    var映射;
    函数初始化(){
    var纬度=21.1202644,
    经度=79.0418986,
    半径=8000,
    中心=新的google.maps.LatLng(纬度、经度),
    映射选项={
    中心:中心,,
    缩放:10,
    滚轮:错误
    };
    map=new google.maps.map(document.getElementById(“map”)、mapOptions);
    设置标记(中心、半径、地图);
    }
    功能设置标记(中心、半径、地图){
    var json=placeid_json;
    for(var i=0,length=json.length;i”+result.name);
    信息窗口。打开(地图、标记);
    });
    })(标记、数据);
    }
    google.maps.event.addDomListener(窗口“加载”,初始化)
    
    html,
    身体,
    #地图{
    身高:100%;
    宽度:100%;
    边际:0px;
    填充:0px
    }
    
    
    我需要编辑这个变量吗纬度=21.1202644,经度=79.0418986,半径=8000,中心=new google.maps.LatLng(纬度,经度),地图选项={中心:中心,缩放:10,滚轮:false};getting error Uncaught TypeError:无法读取未定义的属性“lat”。问题是所有变量都是其函数的局部变量。您需要至少创建
    映射
    边界
    变量全局函数,或将它们作为参数在函数之间传递,以便在需要的任何地方都可以访问它们。如何使用此方法删除标记?请参阅:
    function createMarker(data, map) {
      var service = new google.maps.places.PlacesService(map);
      service.getDetails({
        placeId: data.placeid
      }, function(result, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
          alert(status);
          return;
        }
        var marker = new google.maps.Marker({
          map: map,
          place: {
            placeId: data.placeid,
            location: result.geometry.location
          }
          //position: result.geometry.location
        });
        bounds.extend(result.geometry.location);
        map.fitBounds(bounds);
        infoBox(map, marker, data, result);
      });
    }