2016-05-31 82 views
1

即時通訊工作在谷歌地圖的事情,我需要訪問setZoom方法時點擊自定義按鈕。問題是setZoom()變得不確定。我已經加載了所有的依賴項(地圖渲染和我的$ scope.zoom運行)。我已經測試過了。任何人都知道該怎麼辦?有角度的谷歌地圖。訪問地圖默認功能

HTML:

<div ng-app="app" ng-controller="MapController"> 
<ui-gmap-google-map center="map.center" zoom="map.zoom"> 
    <ui-gmap-map-control template="../pages/partials/map-controls.html" controller="MapController" position="RIGHT_BOTTOM"> 
    </ui-gmap-map-control> 
</ui-gmap-google-map> 
</div> 

JS:

$scope.map = { 
     center: { 
      latitude: 67.855800, 
      longitude: 20.225282 
     }, 
     zoom: 13, 
     options: { 
      scrollwheel: false, 
      disableDefaultUI: true, 
      streetViewControl: false, 
      backgroundColor: '#f2f2f2' 
     } 
    }; 
    $scope.zoom = function($event) { 
     $event.preventDefault(); 
     //the problem is here. $scope.map.setZoom() gets undefined. 
     $scope.map.setZoom(9); 
    }; 

回答

0

我想你想打電話給

$scope.map.control.getGMap().setZoom(9); 

代替

$scope.map.setZoom(9); 

參見示例控制器的詳細信息 - https://github.com/angular-ui/angular-google-maps/blob/cbd17a3a225dfe543faa124eb295e66db566f27c/example/assets/scripts/controllers/example.js

---更新例如

HTML:

<div ng-app="app" ng-controller="MapController"> 
<ui-gmap-google-map 
    center="map.center" 
    zoom="map.zoom"   
    options="map.options" 
    control="map.control> 
    <ui-gmap-map-control 
    template="../pages/partials/map-controls.html" 
    controller="MapController" 
    position="RIGHT_BOTTOM"> 
    </ui-gmap-map-control> 
</ui-gmap-google-map> 
</div> 

JS:

scope.map = { 
     center: { 
      latitude: 67.855800, 
      longitude: 20.225282 
     }, 
     control: {}, 
     zoom: 13, 
     options: { 
      scrollwheel: false, 
      disableDefaultUI: true, 
      streetViewControl: false, 
      backgroundColor: '#f2f2f2' 
     } 
    }; 
    $scope.zoom = function($event) { 
     $event.preventDefault(); 
     //the problem is here. $scope.map.setZoom() gets undefined. 
     $scope.map.setZoom(9); 
    }; 

---添加例如

angular 
 
    .module('app', ['uiGmapgoogle-maps']) 
 
    .controller('MapController', mapController) 
 

 

 
mapController.$inject = ['$scope', 'uiGmapIsReady']; 
 

 
function mapController($scope, uiGmapIsReady) { 
 
    
 
    $scope.map = { 
 
    center: { 
 
     latitude: 67.855800, 
 
     longitude: 20.225282 
 
    }, 
 
    control: {}, 
 
    zoom: 13, 
 
    options: { 
 
     scrollwheel: false, 
 
     disableDefaultUI: true, 
 
     streetViewControl: false, 
 
     backgroundColor: '#f2f2f2' 
 
    } 
 
    }; 
 
    
 
    $scope.zoom = function($event) { 
 
    $scope.map.control.getGMap().setZoom(9); 
 
    }; 
 
     
 
    uiGmapIsReady 
 
    .promise() 
 
    .then(function (maps) { 
 
     // ready to manip map 
 
    }); 
 
};
html, body, #map_canvas { 
 
      height: 200px; 
 
      width: 200px; 
 
      margin: 0px; 
 
     } 
 

 
     #map_canvas { 
 
      position: relative; 
 
     } 
 

 
     .angular-google-map-container { 
 
      position: absolute; 
 
      top: 20px; 
 
      bottom: 0; 
 
      right: 0; 
 
      left: 0; 
 
     }
<!DOCTYPE html> 
 
    <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script> 
 
    <script src="https://code.angularjs.org/1.3.14/angular.js"></script> 
 
    <script src="http://rawgit.com/angular-ui/angular-google-maps/2.0.X/dist/angular-google-maps.js"></script> 
 
    <script type='text/javascript' src='script.js'></script> 
 

 

 

 
<div ng-app='app' id='map_canvas' ng-controller='MapController'> 
 
    <button ng-click='zoom()'>Set Zoom</button> 
 
    <ui-gmap-google-map 
 
    center="map.center" 
 
    zoom="map.zoom" 
 
    control="map.control"> 
 
    </ui-gmap-google-map> 
 
</div>

+0

謝謝!但我得到「map.control是未定義的」。 – user2952238

+0

我已經更新了您的示例以注入'map.control',當您嘗試調用它時,它不應該是未定義的 –

+0

我實現了更改並執行了此操作:console.log($ scope.map.control);其中得到undefined .. – user2952238