在我们创建指令之前,我们需要添加Google APIs 引用到页面中:
|
1
2
3
4
5
6
7
|
<!-- required to use Google maps --><script type="text/javascript"src="https://maps.googleapis.com/maps/api/js?sensor=true"></script> |
接下来,我们创建指令:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
var app = angular.module("app", []);app.directive("appMap", function () {return {restrict: "E",replace: true,template: "<div></div>",scope: {center: "=", // Center point on the mapmarkers: "=", // Array of map markerswidth: "@", // Map width in pixels.height: "@", // Map height in pixels.zoom: "@", // Zoom level (from 1 to 25).mapTypeId: "@" // roadmap, satellite, hybrid, or terrain}, |
center 属性进行了双向绑定。这个应用可以改变地图中心和交互地图(当用户通过鼠标按钮选择地图位置时)。同时,地图也会在用户通过滚动选择地图位置时通知应用更新当前显示位置。
markers 属性被定义为引用因为它是数组形式,把它序列化为字符串比较耗时。link 方法可以实现以下功能:
1. 初始化地图
2. 在用户视图变量更改时更新地图
3. 监听事件
以下是实现代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
link: function (scope, element, attrs) {var toResize, toCenter;var map;var currentMarkers;// listen to changes in scope variables and update the controlvar arr = ["width", "height", "markers", "mapTypeId"];for (var i = 0, cnt = arr.length; i < arr.length; i++) {scope.$watch(arr[i], function () {if (--cnt <= 0)updateControl();});}// update zoom and center without re-creating the mapscope.$watch("zoom", function () {if (map && scope.zoom)map.setZoom(scope.zoom * 1);});scope.$watch("center", function () {if (map && scope.center)map.setCenter(getLocation(scope.center));}); |
监测方法正如我们在文章开始时描述的,变量发生变化后,它将调用updateControl 方法。updateControl 方法实际上使用selected 选项创建了新的地图。
"zoom" 和 "center" 变量将被分别处理,因为我们不希望每次在用户选择或缩放地图时都重新创建地图。这两���方法检测地图是否重新创建还是仅仅是简单的更新。
以下是updateControl 方法的实现方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// update the controlfunction updateControl() {// get map optionsvar options = {center: new google.maps.LatLng(40, -73),zoom: 6,mapTypeId: "roadmap"};if (scope.center) options.center = getLocation(scope.center);if (scope.zoom) options.zoom = scope.zoom * 1;if (scope.mapTypeId) options.mapTypeId = scope.mapTypeId;// create the map and update the markersmap = new google.maps.Map(element[0], options);updateMarkers();// listen to changes in the center property and update the scopegoogle.maps.event.addListener(map, 'center_changed', function () {if (toCenter) clearTimeout(toCenter);toCenter = setTimeout(function () {if (scope.center) {if (map.center.lat() != scope.center.lat ||map.center.lng() != scope.center.lon) {scope.center = { lat: map.center.lat(), lon: map.center.lng() };if (!scope.$$phase) scope.$apply("center");}}}, 500);} |
updateControl 方法首先需要接收Scope设置相关参数,接着使用options 创建和初始化地图。这是创建JavaScript指令的常见模式。
创建地图之后,方法会在更新标记的同时添加检测事件,以便监视地图中心位置的变化。该事件会监测当前的地图中心是否和Scope中的相同。如果不同,即会更新scope,调用$apply 方法通知AngularJS属性已经更改。这种绑定方式为双向绑定。
updateMarkers 方法十分的简单,几乎和AngularJS分离,所以我们在这里就不介绍了。
除了这个地图指令特有的功能,这个例子还展示了:
1. 两个过滤器转换坐标为常规数字到地理位置,例如33°38'24"N, 85°49'2"W。
2. 一个地理编码器,转换成地址的地理位置(也是基于谷歌的API)。
3. 使用HTML5的地理定位服务来获取用户当前位置的方法。
Google地图 APIs 是极其丰富的。以下是一些资源入口:
Google地图APIs 文档: https://developers.google.com/maps/documentation/
Google许可条款:https://developers.google.com/maps/licensing
