在选择选项AngularJS上将Google地图居中

时间:2019-06-29 19:47:46

标签: javascript angularjs google-maps

我正在尝试基于下拉选择选项框中的选择来使地图居中。我已经尝试了几个示例,但是还没有将地图定位到新的纬度和经度坐标的中心。

我想使用现有代码进行工作,而无需从另一个例子开始。

我正在尝试的最新示例在下面的centerMap()函数代码中。

请帮助。
 谢谢!

var sites = [{
    name: "ABC",
    FiftyPlus: "0.72",
    GAs: "0",
    AccPerOpp: "20.09",
    AutoPay: "0.44",
    WTR: "100",
    ISE: "Bert Bingham",
    lat: "29.9792183",
    lng: "-90.22268919999999"
  },
  {
    name: "AJAX",
    FiftyPlus: "0.81",
    GAs: "0",
    AccPerOpp: "18.7",
    AutoPay: "0.13",
    WTR: "100",
    ISE: "Bert Bingham",
    lat: "30.0032515",
    lng: "-90.22060119999999"
  },
  {
    name: "CBS",
    FiftyPlus: "0.8",
    GAs: "0",
    AccPerOpp: "16.99",
    AutoPay: "0.23",
    WTR: "100",
    ISE: "Bing Bingle",
    lat: "43.9703011",
    lng: "-90.08290799999999"
  },
  {
    name: "Turner",
    FiftyPlus: "0.78",
    GAs: "0",
    AccPerOpp: "17.68",
    AutoPay: "0.23",
    WTR: "100",
    ISE: "Bing Bingle",
    lat: "43.004759",
    lng: "-90.17053299999999"
  },
];

// Angular应用模块和控制器

angular.module('mapsApp', [])
  .controller('MapCtrl', function($scope) {
    var mapOptions = {
      zoom: 7,
      center: new google.maps.LatLng(32.340803, -89.4855946),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var mapOptions1 = {
      zoom: 9,
      center: new google.maps.LatLng(31.3186154, -85.8286258),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    $scope.map = new google.maps.Map(document.getElementById('map'), mapOptions);

    function centerMap() {

      if (document.getElementById("ISEs").value === 'Bert Bingle') {
        $scope.map = new google.maps.Map(document.getElementById('map'), mapOptions1);
      }
    }

    $scope.markers = [];

    var infoWindow = new google.maps.InfoWindow();
    /*var icon = {
            url: "image/store.svg", // url
            scaledSize: new google.maps.Size(25, 25), // scaled size
            origin: new google.maps.Point(0,0), // origin
            anchor: new google.maps.Point(0, 0) // anchor
        };

    */
    var createMarker = function(info) {
      var marker = new google.maps.Marker({
        map: $scope.map,
        position: new google.maps.LatLng(info.lat, info.lng),
        title: info.name,
        icon: {
          url: "http://maps.google.com/mapfiles/ms/icons/green-dot.png"
        }
      });
      marker.content = '<div class="iw-ISE">' + info.ISE + '</div>' +
        '<div class="iw-labels">' + "GAs: " + info.GAs + '</div>' +
        '<div class="iw-labels">' + "Auto Pay: " + info.AutoPay + '</div>' +
        '<div class="iw-labels">' + "AccPerOpp:$ " + info.AccPerOpp + '</div>' +
        '<div class="iw-labels">' + "Fifty Plus: " + info.FiftyPlus + '</div>' +
        '<div class="iw-labels">' + "WTR: " + info.WTR + '</div>' +
        '<p>' + info.address + '</p>'

      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent('<h2 class ="title">' + marker.title + '</h2>' + marker.content);
        infoWindow.open($scope.map, marker);
      });
      $scope.markers.push(marker);
    }

    for (i = 0; i < sites.length; i++) {
      createMarker(sites[i]);
    }

    $scope.openInfoWindow = function(e, selectedMarker) {
      e.preventDefault();
      google.maps.event.trigger(selectedMarker, 'click');
    }

    $scope.clearMarkers = function() {
      for (var i = 0; i < $scope.markers.length; i++) {
        $scope.markers[i].setMap(null);
      }
      $scope.markers.length = 0;
    }

    $scope.filterMarkers = function() {
      //1.select filtered cities
      var filteredISE;
      var ISEName = $scope.data.singleSelect;
      if (ISEName == '0') {
        filteredISE = sites;
      } else {
        filteredISE = sites.filter(function(c) {
          if (c.ISE == ISEName)
            return c;
        });
      }
      //2.update markers on map
      $scope.clearMarkers();
      for (i = 0; i < filteredISE.length; i++) {
        createMarker(filteredISE[i]);
      }
    }
  });
<!DOCTYPE html>
<html>

<head>
  <title>Map</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, minimum-scale=1">
  <meta charset="utf-8">
  <link rel="stylesheet" href="CSS/bootstrap.min.css">
  <link rel="stylesheet" href="CSS/style.css">
</head>
<body>
  <div ng-app="mapsApp" ng-controller="MapCtrl">
    <div align="center" class="input-w">
      <div class="gulf">Gulf States</div>
      <fieldset>
        <label>ISE: </label>
        <select id="ISEs" class="dropdown" name="singleSelect" ng-model="data.singleSelect" ng-change="filterMarkers()" ng-change="centerMap()">
          <option value="0">all</option>
          <option id="1" value="Bert Bingham">Bert Bingham</option>
          <option id="2" value="Bing Bingle">Bert Bingle</option>
        </select><br><br>
      </fieldset>
    </div>
    <div id="map"></div>
    <a href="#" ng-click="openInfoWindow($event, marker)">{{marker.title}}</a>
  </div>
  <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAz-1j7m4EfcSFL9o_L4BV7OPyGWNlzpbI&sensor=false" onerror="googleError()"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
  <script src="js/app.js"></script>
  <script src="js/lib/jquery.min.js"></script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的代码中有一些问题:

  1. 当调用控制器的功能时,需要在控制器的作用域上对其进行定义。
    所以不仅仅是声明函数

    function centerMap() {
    

    在控制器上声明

    $scope.centerMap = function () {
    
  2. 就像@georgeawg所说的那样,您在ng-change标签上有两个select指令,您可以像这样组合它们:

    ng-change="centerMap();filterMarkers();"
    
  3. 您正在检查(在if中,它的值是Bert Bingle,但它永远不是那个值,因为选项{{1}的(属性)value }是Bert Bingle。此外,您应该检查范围而不是DOM,因此正确的检查是

    Bing Bingl
  4. 最后,您无需创建新地图即可更改地图的设置,setOptions有一个api。

    所以,不用打电话

    if ($scope.data.singleSelect === 'Bing Bingle') {
    

    这样打电话

    $scope.map = new google.maps.Map(document.getElementById('map'),mapOptions1);
    

工作示例

https://jsbin.com/cuqigaz/edit?js,output