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

Java开发人员

在angularjs应用中使用ngMap
我正在显示带有多个标记的google-map,onclick of Open map! button并且地址数组im中的第一个地址以im为中心。

这是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>

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

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

Allenhwkim

ngMap最近(1.10.0)包含了地图属性“缩放到包含标记”。您可以在这里找到示例,https://rawgit.com/allenhwkim/angularjs-google-maps/master/testapp/map_zoom_to_include_markers.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

Google Maps Javascript API V3中的旋转标记

来自分类Dev

Google Maps Javascript API v3的标记

来自分类Dev

Google Maps Javascript API V3中的旋转标记

来自分类Dev

使用Google Maps Javascript API v3数据层设置多个GeoJson文件的样式

来自分类Dev

如何使用Google Maps Javascript API V3在加载时设置infoWindow内容

来自分类Dev

如何使用Google Maps Javascript API V3在加载时设置infoWindow内容

来自分类Dev

使用Google Maps JavaScript API v3和Geocoding API映射多个位置

来自分类Dev

带有Rhomobile的Google Maps Javascript V3 API

来自分类Dev

Google Maps JavaScript API v3 /数据层/ MarkerClusterer

来自分类Dev

Google Maps Javascript API V3:标记具有相同的标题

来自分类Dev

Google Maps JavaScript API v3更改标记并添加卫星视图

来自分类Dev

地图标记无法呈现-Phonegap中的Google Maps Javascript API v3

来自分类Dev

动画化不同的标记API V3 Google Maps

来自分类Dev

标记位置未获取-Google Maps API v3

来自分类Dev

标记拖动事件Google Maps API V3

来自分类Dev

Google Maps API v3标记问题

来自分类Dev

Google Maps API v3唯一标记JS

来自分类Dev

Google Maps JS api v3延迟拖拽标记

来自分类Dev

在Google Maps API v3中绘制多个圆圈

来自分类Dev

多个图块问题Google Maps API v3 JS

来自分类Dev

在maps.google.com上缩放比在Google Maps API v3上平滑

来自分类Dev

在Google Maps Javascript API v3中一次拖动多个多边形

来自分类Dev

如何获取Google Map Api v3密钥?

来自分类Dev

Google Map API V3:如何禁用植被?

来自分类Dev

Google Map API V3:如何禁用植被?

来自分类Dev

缩放后会显示隐藏的标记-API Google Maps V3

来自分类Dev

如何在Ionic / Cordova应用程序中保护我的Google Maps Javascript V3 API密钥?

来自分类Dev

如何在Ionic / Cordova应用程序中保护Google Maps Javascript V3 API密钥?

Related 相关文章

  1. 1

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

  2. 2

    Google Maps Javascript API V3中的旋转标记

  3. 3

    Google Maps Javascript API v3的标记

  4. 4

    Google Maps Javascript API V3中的旋转标记

  5. 5

    使用Google Maps Javascript API v3数据层设置多个GeoJson文件的样式

  6. 6

    如何使用Google Maps Javascript API V3在加载时设置infoWindow内容

  7. 7

    如何使用Google Maps Javascript API V3在加载时设置infoWindow内容

  8. 8

    使用Google Maps JavaScript API v3和Geocoding API映射多个位置

  9. 9

    带有Rhomobile的Google Maps Javascript V3 API

  10. 10

    Google Maps JavaScript API v3 /数据层/ MarkerClusterer

  11. 11

    Google Maps Javascript API V3:标记具有相同的标题

  12. 12

    Google Maps JavaScript API v3更改标记并添加卫星视图

  13. 13

    地图标记无法呈现-Phonegap中的Google Maps Javascript API v3

  14. 14

    动画化不同的标记API V3 Google Maps

  15. 15

    标记位置未获取-Google Maps API v3

  16. 16

    标记拖动事件Google Maps API V3

  17. 17

    Google Maps API v3标记问题

  18. 18

    Google Maps API v3唯一标记JS

  19. 19

    Google Maps JS api v3延迟拖拽标记

  20. 20

    在Google Maps API v3中绘制多个圆圈

  21. 21

    多个图块问题Google Maps API v3 JS

  22. 22

    在maps.google.com上缩放比在Google Maps API v3上平滑

  23. 23

    在Google Maps Javascript API v3中一次拖动多个多边形

  24. 24

    如何获取Google Map Api v3密钥?

  25. 25

    Google Map API V3:如何禁用植被?

  26. 26

    Google Map API V3:如何禁用植被?

  27. 27

    缩放后会显示隐藏的标记-API Google Maps V3

  28. 28

    如何在Ionic / Cordova应用程序中保护我的Google Maps Javascript V3 API密钥?

  29. 29

    如何在Ionic / Cordova应用程序中保护Google Maps Javascript V3 API密钥?

热门标签

归档