使用angular-google-maps设置Google地图边界

Vit*_*lho 5 google-maps google-maps-api-3 angularjs angular-google-maps

我正在尝试将界限设置为Google Maps实例,但没有成功.我正在使用AngularJS和angular-google-maps模块.我的最后一次尝试是:

<div id="map_canvas">
    <ui-gmap-google-map 
        center="map.center" 
        zoom="map.zoom"
        bounds="map.bounds"
        options="options">

        <div ng-repeat="d in devicesMarkers track by d.id">

            <ui-gmap-marker
                idkey="d.id"
                coords="d.center"
                options="d.options">
            </ui-gmap-marker>
            <ui-gmap-circle
                center="d.center"
                stroke="d.circle.stroke"
                fill="d.circle.fill"
                radius="d.circle.radius"
                visible="d.options.visible">
            </ui-gmap-circle>

        </div>

    </ui-gmap-google-map>
</div>
Run Code Online (Sandbox Code Playgroud)

在我app.js写的:

(...)
.controller('TaihMapController', ['$scope', '$log', 'uiGmapGoogleMapApi', function($scope, $log, GoogleMapApi) {
    $scope.devicesMarkers = devices;
    $scope.map = {
        center: { latitude: devices[0].center.latitude, longitude: devices[0].center.longitude },
        zoom: 12,
        bounds: {}
        // fit: true,
    };
    GoogleMapApi.then(function(maps) {
        $log.debug(maps);
        var myBounds = new maps.LatLngBounds();
        for (var k = 0; k < devices.length; k++) {
            var _tmp_position = new maps.LatLng(
                devices[k].center.latitude, 
                devices[k].center.longitude);
            myBounds.extend(_tmp_position);
        }
    $scope.map.bounds = myBounds;
    $scope.googleVersion = maps.version;
    // $scope.bounds = myBounds;
    // $scope.zoom = maps.getZoom();
    // maps.fitBounds(myBounds);
    // $scope.bounds = maps.getBounds();

});
Run Code Online (Sandbox Code Playgroud)

该功能maps.fitBounds()不存在.我尝试了这种$scope.map.bounds方法,但没有像我想的那样回应.

Ala*_*lan 8

如果你正在使用angular-google-map的"markers"指令,只需添加fit ="true"属性(ui-gmap-markers).

示例:

<ui-gmap-google-map center="map.center" zoom="map.zoom" options="map.options"> <ui-gmap-markers fit="true" models="markers" coords="'self'"></ui-gmap-markers> </ui-gmap-google-map>

资料来源:https: //stackoverflow.com/a/29707965


Meh*_*ban 3

根据文档,指令边界与谷歌地图边界略有不同。您应该使用自己的和显式设置northeast和属性。southwestlatitudelongitude

下面应该可以工作:

$scope.map.bounds = {
    northeast: {
        latitude: myBounds.getNorthEast().lat(),
        longitude: myBounds.getNorthEast().lng()
    },
    southwest: {
        latitude: myBounds.getSouthWest().lat(),
        longitude: myBounds.getSouthWest().lng()
    }
};
Run Code Online (Sandbox Code Playgroud)