简体   繁体   English

如何动态设置最大缩放级别以适合Google地图中的标记列表?

[英]How to set the maximum zoom level dynamically to fit a list of markers in google-map?

This is my code, I've used angular-google-map API for it. 这是我的代码,我使用了angular-google-map API。 Here I try to set the zoom level of 20 statically, but as per my requirement, in some place I need 17, somewhere 18 zoom level. 在这里,我尝试将静态缩放级别设置为20,但根据我的要求,在某些地方我需要17缩放级别。 So I want to dynamically generate the maximum possible zoom level to fit all the markers. 因此,我想动态生成最大可能的缩放级别以适合所有标记。

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);

Remove the zoom level. 删除缩放级别。 It will automatically adjust the value such that all markers are covered. 它将自动调整值,以覆盖所有标记。 Read this section - https://developers.google.com/maps/documentation/static-maps/intro#Markers 阅读本节-https://developers.google.com/maps/documentation/static-maps/intro#Markers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM