javascript - 在 Ionic 中使用多个标记的谷歌地图自动缩放

标签 javascript angularjs google-maps ionic-framework

<分区>

指令.js:

.directive('map', function() {
  return {
    restrict: 'E',
    scope: {
      onCreate: '&'
    },
    link: function ($scope, $element, $attr) {
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(43.07493, -89.381388),
          zoom: 16,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map($element[0], mapOptions);

        $scope.onCreate({map: map});

        // Stop the side bar from dragging when mousedown/tapdown on the map
        google.maps.event.addDomListener($element[0], 'mousedown', function (e) {
          e.preventDefault();
          return false;
        });
      }

      if (document.readyState === "complete") {
        initialize();
      } else {
        google.maps.event.addDomListener(window, 'load', initialize);
      }
    }
  }
});

HTML:

<map on-create="mapCreated(map)" data-tap-disabled="true"></map>

controller.js:

//Map controller
$scope.mapCreated = function (map) {
    $scope.map = map;
    $scope.map.setCenter(new google.maps.LatLng(latitude1, longitude1));
    createMarker(latitude1, longitude1, icon1);
    createMarker(latitude2, longitude2, icon2);             

    //Create marker
    var createMarker = function (lat, long, icon) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, long),
            map: map,
            icon: icon,
        });
    }
};

假设“latitude1、longitude1、latitude2、longitude2、icon1 和 icon2”已经初始化。

我的代码没有出错,但它没有缩小 map 以显示所有标记。我之前做过一些研究,但找不到合适的答案。感谢您的帮助。

最佳答案

你需要禁用data-tap http://ionicframework.com/docs/api/page/tap/允许 googlemaps 处理点击事件。

<ion-view>
...
<ion-content scroll="false">
    <map on-create="mapCreated(map)" data-tap-disabled="true"></map>
</ion-content>
...
</ion-view>

关于javascript - 在 Ionic 中使用多个标记的谷歌地图自动缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34061678/

相关文章:

android - 如何在不点击的情况下在谷歌地图可组合标记上显示标题和 fragment ?

javascript - JQuery位置选择器,如何设置距起点的最大允许距离

javascript - 什么是 "app.router"express.js

javascript - AngularJS - 获取 'undefined'

javascript - PHP google map 按类型获取纬度和经度

javascript - ng-模型在 ng-transclude 中

javascript - Angular js ng 显示不工作

javascript - 使用 Splice() 函数,如何删除特定的数组元素?

javascript - 如何在 FireBase 和 Angular 中根据用户隐藏元素

javascript - 当我尝试访问不存在的属性时,为什么 javascript 没有抛出类型错误?