如何动态设置最大缩放级别以适应google-map中的标记列表?

时间:2016-03-18 11:50:01

标签: javascript google-maps-api-3 angular-google-maps

这是我的代码,我使用了angular-google-map API。在这里,我尝试静态设置20的缩放级别,但根据我的要求,在某些地方我需要17,某处有18个缩放级别。所以我想动态生成最大可能的缩放级别以适应所有标记。

var deviceCords = [];
                var bounds = new google.maps.LatLngBounds();
                $scope.$parent.$parent.$parent.map.zoom = 20;
                var devicePromise = SiteService.getDevicesForSite(marker).$promise;
                devicePromise.then(function(deviceData){
                    var markerCategory = marker.category;
                    if(markerCategory === "Public Venues")
                        markerCategory = "Venue";
                    else if(markerCategory === "Public Transit")
                        markerCategory = "Trans";
                    $scope.$parent.$parent.$parent.categoryFilter(markerCategory);
                   var devices = deviceData.devices;
                   var markers = $scope.$parent.$parent.$parent.markers;
                   markers.splice(0, markers.length);
                   angular.forEach(devices, function(device){
                       deviceCords.push(new google.maps.LatLng(device.latitude, device.longitude));
                       var deviceMarker = {
                           id : device.deviceId,
                           name : device.name,
                           latitude : device.latitude,
                           longitude: device.longitude,
                           category : device.category,
                           icon: "assets/images/device.png"
                       };
                       markers.push(deviceMarker);
                   });

                    angular.forEach(deviceCords, function(deviceCord){
                        bounds.extend(deviceCord);
                    });
                   var latlng = bounds.getCenter();
                   $scope.$parent.$parent.$parent.map.center = {
                       latitude: latlng.lat().toString().substr(0, 20),
                       longitude: latlng.lng().toString().substr(0, 20)
                   };
                   $scope.$parent.$parent.$parent.map.fitBounds(bounds);

1 个答案:

答案 0 :(得分:0)

删除缩放级别。它将自动调整值,以便覆盖所有标记。阅读本节 - https://developers.google.com/maps/documentation/static-maps/intro#Markers