AngularJSAngularJS指令封装高德地图组件

1 概述

     
公司走门户原来是基于AngularJS指令封装的百度地图组件,用于登录、签退、定位等功效,在动用过程中发现百度地图频繁之弹来广告,所以打算再引用其它地图组件,最后决定基于AngularJS指令来封装高德地图组件,本文主要和大家分享自己之上思路与支付具体经过。

注意:本文假定读者基本掌握html、css、js以及angularjs,了解百度、高德或者腾讯地图JS
API的基本概念。

2 开发思路

      
由于事先没有开了地图组件,所以当付出之前用做好学习计划,预想下开发组件时或者会见遇上的技术点与难题,之后发指向的拓学习。首先学高德地图api,把大概的基本功能实现出来,再开一个带按钮的,保证在本地可以运作,然后上angularjs的授命,参考公司依据百度地图样例定位指令,基于angularjs封装高德地图组件,先基本显示有地图和标明,最后念$watch监听功能的用法,当地图坐标发生转移,使用angularjs里$watch监听属性,在地形图对经度、维度进行动态标记。下面是受解说后底修步骤供大家参考。

3 具体步骤

3.1 高德地图JavaScriptAPI学习

1 运行下面的链接进入高德地图网站

http://id.amap.com/?ref=http%3A%2F%2Flbs.amap.com%2Fdev%2Fkey#/account/info

2
注册一个帐号,并创建一个key,如下图选择一个web端

AngularJS 1

 

AngularJS 2

3 点击下面的链接开始高德地图的入门学习

http://lbs.amap.com/api/javascript-api/example/map/asynchronous-loading-map/

从今左菜单目录看出可以由深至浅的攻,中间闹代码编辑区,右边可以一直看到结果,非常有利于

AngularJS 3

3.2 在本地html页面显示地图样例

1
本地创建html文件,与相关的css文件,将下的代码考贝到该文件内

创建css文件,下面是css代码

html, body {

      margin: 0;

      height: 100%;

      width: 100%;

      position: absolute;

}

 

#container {

      position: absolute;

      top: 0;

      left: 0;

      right: 0;

      bottom: 0;

      width: 100%;

      height: 100%;

}

 

.button-group {

      position: absolute;

      bottom: 20px;

      right: 20px;

      font-size: 12px;

      padding: 10px;

}

 

.button-group .button {

      height: 28px;

      line-height: 28px;

      background-color: #0D9BF2;

      color: #FFF;

      border: 0;

      outline: none;

      padding-left: 5px;

      padding-right: 5px;

      border-radius: 3px;

      margin-bottom: 4px;

      cursor: pointer;

}

.button-group .inputtext {

      height: 26px;

      line-height: 26px;

      border: 1px;

      outline: none;

      padding-left: 5px;

      padding-right: 5px;

      border-radius: 3px;

      margin-bottom: 4px;

      cursor: pointer;

}

  

#tip {

      background-color: #fff;

      padding-left: 10px;

      padding-right: 10px;

      position: absolute;

      font-size: 12px;

      right: 10px;

      top: 20px;

      border-radius: 3px;

      border: 1px solid #ccc;

      line-height: 30px;

}

 

 

.amap-info-content {

      font-size: 12px;

}

 

#myPageTop {

      position: absolute;

      top: 5px;

      right: 10px;

      background: #fff none repeat scroll 0 0;

      border: 1px solid #ccc;

      margin: 10px auto;

      padding:6px;

      font-family: "Microsoft Yahei", "微软雅黑", "Pinghei";

      font-size: 14px;

}

#myPageTop label {

      margin: 0 20px 0 0;

      color: #666666;

      font-weight: normal;

}

#myPageTop input {

      width: 170px;

}

#myPageTop .column2{

      padding-left: 25px;

}

#container {

      height: 50%;

      position: initial;

}

缔造html文件,将下的代码拷贝到html文件中

注意:key的值填写你协调的

<link rel="stylesheet"      href="../org/css/gaodeMap.css" />

<script        src="http://webapi.amap.com/maps?v=1.3&key=837a9bdb426d81b6862135983d1d715c"></script>

<div id="container"></div>

      <div style="height:100px; text-align: center; margin: 50px;">

</div>

<script type="text/javascript">

                    var map = new AMap.Map("container", {

                           resizeEnable : true,

                           zoom : 17

                    });

                    var cpoint=map.getCenter( );

                    var marker = new AMap.Marker({

                           map : map,

                           draggable: true,  //是否可拖动

                           bubble : true

                    })

</script>

下是马上简单独文本的门路关系,为同级关系

AngularJS 4

2
双击gaodeMap.html文件,查看执行结果

AngularJS 5

3.3 为页面添加按钮设置地图标记

修改html文件之代码,将下的代码拷贝到您的html文件被

<link rel="stylesheet"       href="../org/css/gaodeMap.css" />

<script src="http://webapi.amap.com/maps?v=1.3&key=837a9bdb426d81b6862135983d1d715c"></script>

<div id="container"></div>

<div style="height:100px; text-align: center; margin: 50px;">

       <div id="tip"></div>

       <input type="button"   onclick="setMarkerA()" value="设置markerA">

       <input type="button"   onclick="setMarkerB()" value="设置markerB">

 </div>

<script type="text/javascript">

           var map = new AMap.Map("container", {

                     resizeEnable : true,

                     zoom : 17

              });

              var cpoint=map.getCenter( );

              var marker = new AMap.Marker({

                  map : map,

                     draggable: true,  //是否可拖动

                     bubble : true

         })

              setMarkerA=function(){ 

                     var mapOptions={"lng":123.427476,"lat":41.797287};

                     map.setCenter([mapOptions.lng ,mapOptions.lat]);

                     marker.setPosition([mapOptions.lng ,mapOptions.lat]);

              }

              setMarkerB=function(){

                     var mapOptions={"lng":123.42405344705162,"lat":41.798870569210926};

                     map.setCenter([mapOptions.lng ,mapOptions.lat]);

                     marker.setPosition([mapOptions.lng ,mapOptions.lat]);

              }

</script>

末了效果如下

AngularJS 6

3.4 学习AngularJS指令用法

1
在AngularJS菜鸟教程被读书有关directive的有关知识

点击下面的链接进入学习页面

http://www.runoob.com/try/try.php?filename=try_ng_directive_comment

 

 2 了解AngularJS菜鸟教程被之directive的系文化后达到找引擎学习directive的相干文化,进一步了解directive的用法

AngularJS 7

3
可以参见下面代码,配合搜索引擎学习累了解directive中之系属性之文化:restrict,replace,template,link

.directive("appMap", function () {

        return {

          restrict: "E",

          replace: true,

          template: "<div id=’allMap’></div>",

        scope:{

             ‘options’: ‘=’

        },

          link: function ($scope, element, attrs) {

             var map = new BMap.Map("allMap");

             $scope.$watch("options", function (newValue, oldValue) {

                   var allOverlay = map.getOverlays();

                   

                   if (allOverlay && allOverlay.length > 0){

                          for (var i=0;i < allOverlay.length;i++){

                                 map.removeOverlay(allOverlay[i]);                                  

                          }

                       }

                   

                    if ($scope.options && $scope.options.longitude && $scope.options.latitude){

                          var longitude = $scope.options.longitude;

                          var latitude = $scope.options.latitude;

                           var point = new BMap.Point(longitude,latitude); 

                        map.centerAndZoom(point,17); 

                          var mk = new BMap.Marker(point); 

                          var label = new BMap.Label("我在这里",{offset:new BMap.Size(20,-10)});

                          label.setStyle({

                               "color":"green",

                               "fontSize":"14px",

                               "border":"1",

                               "textAlign":"center"

                          });

                          mk.setLabel(label);

                          map.addOverlay(mk);

                    };

             },true);

       }           

        };

})

3.5 封装AngularJS指令显示地图

参考公司网站的定义功能,用angularjs的艺术以一个略的地图显示出,可以优先不考虑$watch的用法

1.查看企业定点的法子以啊地方

AngularJS 8

AngularJS 9

2.创办一个html文件,将下的代码考入,注意这里导入的css文件,与angular.min.js文件

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

      <link rel="stylesheet"

      href="../css/gaodeMap.css" />

      <script src="http://webapi.amap.com/maps?v=1.3&key=837a9bdb426d81b6862135983d1d715c"></script>

    <script src="../js/angular.min.js"></script>

    </head>

<body>

 

<div ng-app="hd" ng-controller="ctrlA">

<gaode-map options="mapOptions" style="height:400px"></gaode-map>

</div>

 

<script>

    var m = angular.module(‘hd’, []);

    m.directive(‘gaodeMap’, [function () {

        return {

            restrict: ‘E’,

                    replace:true,

            template: ‘<div id="container"></div>’,

                    scope: {

                options:’=’

            },

                    link: function ($scope, elem, attr) {

                           var map = new AMap.Map("container", {

                                  resizeEnable : true,

                                  zoom : 17

                           });

                           var marker = new AMap.Marker({

                                         map : map,

                                         bubble : true ,

                                         content: ‘<div class="marker-route marker-marker-bus-from"></div>’  //自定义点标记覆盖物内容,

                           })

                               

                           marker.setLabel({

                                  offset: new AMap.Pixel(20, 0),

                                  content: "我在这里"

                           });

                           $scope.$watch("options", function (newValue, oldValue) {    

                                  if ($scope.options && $scope.options.lng && $scope.options.lat){

                                         map.setCenter([$scope.options.lng ,$scope.options.lat]);

                                         marker.setPosition([$scope.options.lng ,$scope.options.lat]);

                                  }     

                           },true);

                    }

        }

    }]);

      m.controller("ctrlA",function($scope){

              

      });

</script>

</body>

</html>

3.导入相应的css文件及AngularJS文件

下面是css文件之代码

html, body {

       margin: 0;

       height: 100%;

       width: 100%;

       position: absolute;

}

 

#container {

       position: absolute;

       top: 0;

       left: 0;

       right: 0;

       bottom: 0;

       width: 100%;

       height: 100%;

}

 

.button-group {

       position: absolute;

       bottom: 20px;

       right: 20px;

       font-size: 12px;

       padding: 10px;

}

 

.button-group .button {

       height: 28px;

       line-height: 28px;

       background-color: #0D9BF2;

       color: #FFF;

       border: 0;

       outline: none;

       padding-left: 5px;

       padding-right: 5px;

       border-radius: 3px;

       margin-bottom: 4px;

       cursor: pointer;

}

.button-group .inputtext {

       height: 26px;

       line-height: 26px;

       border: 1px;

       outline: none;

       padding-left: 5px;

       padding-right: 5px;

       border-radius: 3px;

       margin-bottom: 4px;

       cursor: pointer;

}

 #tip {

       background-color: #fff;

       padding-left: 10px;

       padding-right: 10px;

       position: absolute;

       font-size: 12px;

       right: 10px;

       top: 20px;

       border-radius: 3px;

       border: 1px solid #ccc;

       line-height: 30px;

}

 

.amap-info-content {

       font-size: 12px;

}

 

#myPageTop {

       position: absolute;

       top: 5px;

       right: 10px;

       background: #fff none repeat scroll 0 0;

       border: 1px solid #ccc;

       margin: 10px auto;

       padding:6px;

       font-family: "Microsoft Yahei", "微软雅黑", "Pinghei";

       font-size: 14px;

}

#myPageTop label {

       margin: 0 20px 0 0;

       color: #666666;

       font-weight: normal;

}

#myPageTop input {

       width: 170px;

}

#myPageTop .column2{

       padding-left: 25px;

}

#container {

       height: 1000px;

       position: initial;

}

.amap-marker-label {

                     border: 0px;

                     color: #0bc00f;

                     background: rgba(255, 255, 255, 0);

              }

.amap-marker .marker-route {

            position: absolute;

            width: 40px;

            height: 44px;

            color: #e90000;

            background: url(http://webapi.amap.com/theme/v1.3newpc/poi-1.png) no-repeat;

            cursor: pointer;

}

.amap-marker .marker-marker-bus-from {

            background-position: -334px -18px;

}

4.脚是导入的angularjs的库文件,拷贝出去放到自己的计算机里,放入相应目录

AngularJS 10

5.底下是本人的路线供参考

AngularJS 11

6.用浏览器打开该文件查看,f12开辟控制高切换至手机模式查看预览效果

AngularJS 12

3.6 扩展$watch监听动态显示位置

读$watch的用法,监听坐标数据变动状态,添加一个按钮为地图设置marker标记,完善地方功能。

1.上网学习有关$watch的连锁用法

AngularJS 13

2.接续参照公司的道,完善地方的效果,为面的效果丰富按钮为地图设置坐标下面为修改后底代码

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

       <link rel="stylesheet"

       href="../css/gaodeMap.css" />

       <script src="http://webapi.amap.com/maps?v=1.3&key=837a9bdb426d81b6862135983d1d715c"></script>

    <script src="../js/angular.min.js"></script>

    <script src="../js/jsjquery1.7.1.js"></script>

</head>

<body>

 

<div ng-app="hd" ng-controller="ctrlA">

<gaode-map options="mapOptions" style="height:400px"></gaode-map>

<input type="button"  ng-click="setMarkerA()" value="设置markerA">

<input type="button"  ng-click="setMarkerB()" value="设置markerB">

</div>

 

<script>

    var m = angular.module(‘hd’, []);

    m.directive(‘gaodeMap’, [function () {

        return {

            restrict: ‘E’,

                     replace:true,

            template: ‘<div id="container"></div>’,

                     scope: {

                options:’=’

            },

                     link: function ($scope, elem, attr) {

                            var map = new AMap.Map("container", {

                                   resizeEnable : true,

                                   zoom : 17

                            });

                            var marker = new AMap.Marker({

                                          map : map,

                                          bubble : true ,

                                          content: ‘<div class="marker-route marker-marker-bus-from"></div>’  //自定义点标记覆盖物内容,

                            })

                                

                            marker.setLabel({

                                   offset: new AMap.Pixel(20, 0),

                                   content: "我在这里"

                            });

                            $scope.$watch("options", function (newValue, oldValue) {    

                                   if ($scope.options && $scope.options.lng && $scope.options.lat){

                                          map.setCenter([$scope.options.lng ,$scope.options.lat]);

                                          marker.setPosition([$scope.options.lng ,$scope.options.lat]);

                                   }     

                            },true);

                     }

        }

    }]);

       m.controller("ctrlA",function($scope){

              $scope.mapOptions={"lng":123.42678393582929,"lat":41.79739087943974};              $scope.setMarkerA=function(){

                     $scope.mapOptions={"lng":123.43223420561884,"lat":41.7987619126648};

              }

              $scope.setMarkerB=function(){

                     $scope.mapOptions={"lng":123.42405344705162,"lat":41.798870569210926};

              }

       });

      

</script>

</body>

</html>

3.底为浏览器被实施后底意义

AngularJS 14

4 私总结 

上外新物,甚至开另外业务之前,首先使旗帜鲜明目标,然后制定计划、分解计划、明确重要、攻克难点。计划而先行总后分,先全局再有的,对粗分过之手续再分叉,把不知情的知识点重点标注,尽可能细化的解释,让过程只是操作、易落地。

以念研讨过程遭到使搞好笔记,把有关学习材料、参考文件存放于一个目,过程文件需要备份。

撞不能够缓解之难关一定要是奋力尝试思考过以后,再夺探听,在询问前如果明了好的题材、要力所能及阐述清楚自己之问题,有时候明确问题怎么阐述下,还并未失去问问别人,可能问题即好解决了。只有思想了之事物、有了实践、有矣交付物、有矣笔记、能够简单明了阐述清楚,才能够算是真正体味。

5 附件说明

下列附件也念过程被理的4个样例文件,由浅入深,逼近最终目标。4单公文都无异,解压至当地,浏览器运行gaodeMap.html文件即可

5.1 附件1,基础演示高德地图

AngularJS 15

附件说明:解压至当地,浏览器运行gaodeMap.html文件即可

运转效果:

AngularJS 16

5.2 附件2,支持标注动态设置

AngularJS 17

 

AngularJS 18

5.3 附件3,AngularJS指令封装地图

AngularJS 19

 

AngularJS 20

5.4 附件4,扩展监听令动态显示位置

AngularJS 21

运行效果:

AngularJS 22

文档及附件下充斥地址:http://pan.baidu.com/s/1mi7F8yG

相关文章