javascript - Angularjs - ng-map - Google Maps Javascript API v3 - 如何为多个标记设置最佳缩放

标签 javascript angularjs google-maps-api-3 angularjs-google-maps ng-map

我正在使用 ngMap 在 angularjs 应用程序中。
我正在显示带有多个标记的 google-map onclick of Open map!按钮和第一个地址出现在我作为 map 中心的地址数组中。

这里是 The Plunk

example.js:

angular.module('plunker', ['ui.bootstrap', 'ngMap']);
var ModalDemoCtrl = function ($scope, $modal, $log) {

    $scope.displayMap = function () {

        $scope.addresses = [{ "address": "Hyderabad" },
        { "address": "Chennai" },
        { "address": "Bangalore" },
        { "address": "Kolkata" },];

        $scope.latlng = [];
        $scope.loop(0, $scope.addresses)
    }

    $scope.loop = function (id, addressesresult) {
        if (id < addressesresult.length) {
            var address = addressesresult[id].address;
            geocoder = new google.maps.Geocoder();
            geocoder.geocode({
                'address': address
            }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    $scope.latlng.push({
                        "lat": results[0].geometry.location.lat(),
                        "lng": results[0].geometry.location.lng(),
                        "title": results[0].formatted_address
                    });

                    if (id == ((addressesresult.length) - 1)) {
                        $scope.openPopup();

                    }
                } else {
                    console.log('Geocode was not successful for the following reason:' + status);
                }
                id = id + 1;
                $scope.loop(id, addressesresult);
            });
        }
    }

    $scope.openPopup = function () {
        var modalInstance = $modal.open({
            templateUrl: 'modal1',
            controller: ModalInstanceCtrl,
            scope: $scope,
            resolve: {
                lat: function () {
                    return $scope.latlng[0].lat;
                },
                lng: function () {
                    return $scope.latlng[0].lng;
                },
                latlng: function () {
                    return $scope.latlng;

                }
            }
        });

    };

};

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

var ModalInstanceCtrl = function ($scope, $modalInstance, lat, lng) {

    $scope.lat = lat;
    $scope.lng = lng;

    $scope.render = true;

    $scope.close = function () {
        $modalInstance.close();
    };
};

index.html:

<!doctype html>
<html ng-app="plunker">

<head>
    <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
    <script src="http://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.min.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="style.css" />
</head>

<body>
    <div ng-controller="ModalDemoCtrl">
        <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title">I'm a modal!</h3>
        </div>
        <div class="modal-body">
            <map ng-if="$parent.render" center="[{{$parent.lat}}, {{$parent.lng}}]" zoom-control="true" zoom="8"> </map>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="ok()">OK</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </script>

        <button class="btn btn-default" ng-click="displayMap()">Open map!</button>

        <script type="text/ng-template" id="modal1">
        <div class="modal-header googlemodal-header">
<button type="button" ng-click="close()" style="opacity:1" class="close" data-dismiss="modal" aria-hidden="true"> <span style="color:red; ">x</span> </button>
            <div align="center"><h3 class="modal-title">Addresses pointing in Google Map  </h3></div>
        </div>
        <div class="modal-body">
            <map ng-if="$parent.render" center="[{{lat}}, {{lng}}]"  zoom="15" zoom-control="true"  style="display:block; width:auto; height:auto;"> 
                <marker ng-repeat="item in latlng" 
            position="{{item.lat}}, {{item.lng}}" 
            title="{{item.title}}"></marker>
            </map>
        </div>
        <div class="modal-footer googlemodal-header">
            <button class="btn btn-primary btn-sm" ng-click="close()">CLOSE</button>
        </div>
    </script>

    </div>
</body>

</html>

所有与地址相关的标记都显示正常。

但是如何为地址数组自动设置最佳缩放比例,这里是我从数据库中获取的这些地址,因此这些地址会根据某些条件针对每个请求进行更改。

最佳答案

ngMap 最近 (1.10.0) 包含一个 map 属性“zoom-to-include-markers”。你可以在这里找到一个例子,https://rawgit.com/allenhwkim/angularjs-google-maps/master/testapp/map_zoom_to_include_markers.html

关于javascript - Angularjs - ng-map - Google Maps Javascript API v3 - 如何为多个标记设置最佳缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30050392/

相关文章:

javascript - super 菜单的第三级不起作用

javascript - 构建自定义 jQuery 选择

javascript - ng-repeat 与动态数组 : view not updated

javascript - Angular 名称不刷新

javascript - 限制 google 位置条目到 AngularJS 中的建议列表

javascript - 如何使用 LinkedIn javascript sdk 检索包括所有字段的职位列表?

javascript - Vue 和重复的 Prop

javascript - 使用空值集触发 Bootstrap 工具提示

google-maps-api-3 - 改变圆的位置?

javascript - 当绘图管理器对标记处于事件状态时,自动单击 map 中心