分享

Arcgis for js加载百度地图

 昵称QAb6ICvc 2019-03-07

2018年05月09日 09:21:44 纸心GIS 阅读数:1005

通过学习了一段时间的arcgis for js,让我来讲一下如何在arcgis for js加载百度地图

加载的结果显示:

                                                                           地图

                                                                  影像 - 无标注

                                                                           影像 - 有标注

制作步骤:

首先,我是在学习了https://blog.csdn.net/gisshixisheng/article/details/44853709这篇文章,代码大多都是那里的,参数也都没错,但是复制过去还是打不开,这个之后我会讲的。

其次,就是_百度地图在柯林斯调用的有地图在切片,影像切片,以及道路等POI切片,我将之用TiledMapServiceLayer做了扩展,成了BDAnoLayer,BDVecLayer,BDimgLayer三个图层,其代码如下:(通俗的意思就是在html同目录的新建一个文件夹,然后建立三个JavaScript文件分别是BDAnoLayer,BDVecLayer,BDImgLayer

BDAnoLayer.js

  1. define(["dojo/_base/declare",

  2. "esri/layers/TiledMapServiceLayer",

  3. "esri/geometry/Extent",

  4. "esri/SpatialReference",

  5. "esri/layers/TileInfo"

  6. ], function (declare, TiledMapServiceLayer, Extent, SpatialReference, TileInfo) {

  7. return declare(TiledMapServiceLayer, {

  8. constructor: function () {

  9. this.spatialReference = new SpatialReference({ wkid: 102100 });

  10. this.initialExtent = (this.fullExtent = new Extent(-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892, this.spatialReference));

  11. this.scale = [591657527.591555,295828763.795777,147914381.897889,73957190.948944,36978595.474472,18489297.737236,9244648.868618,

  12. 4622324.434309,2311162.217155,1155581.108577,577790.554289,288895.277144,144447.638572,72223.819286,

  13. 36111.9096437,18055.9548224,9027.977411,4513.988705,2256.994353,1128.497176];

  14. this.resolution = [156543.033928,78271.5169639999,39135.7584820001,19567.8792409999,9783.93962049996,4891.96981024998,2445.98490512499,

  15. 1222.99245256249,611.49622628138,305.748113140558,152.874056570411,76.4370282850732,38.2185141425366,19.1092570712683,9.55462853563415,

  16. 4.77731426794937,2.38865713397468,1.19432856685505,0.597164283559817,0.298582141647617];

  17. this.tileInfo = new TileInfo({

  18. "rows": 256,

  19. "cols": 256,

  20. "compressionQuality": 90,

  21. "origin": {

  22. "x": -20037508.3427892,

  23. "y": 20037508.3427892

  24. },

  25. "spatialReference": this.spatialReference,

  26. "lods": [{ "level": 0, "resolution": this.resolution[0], "scale": this.scale[0] },

  27. { "level": 1, "resolution": this.resolution[1], "scale": this.scale[1] },

  28. { "level": 2, "resolution": this.resolution[2], "scale": this.scale[2] },

  29. { "level": 3, "resolution": this.resolution[3], "scale": this.scale[3] },

  30. { "level": 4, "resolution": this.resolution[4], "scale": this.scale[4] },

  31. { "level": 5, "resolution": this.resolution[5], "scale": this.scale[5] },

  32. { "level": 6, "resolution": this.resolution[6], "scale": this.scale[6] },

  33. { "level": 7, "resolution": this.resolution[7], "scale": this.scale[7] },

  34. { "level": 8, "resolution": this.resolution[8], "scale": this.scale[8] },

  35. { "level": 9, "resolution": this.resolution[9], "scale": this.scale[9] },

  36. { "level": 10, "resolution": this.resolution[10], "scale": this.scale[10] },

  37. { "level": 11, "resolution": this.resolution[11], "scale": this.scale[11] },

  38. { "level": 12, "resolution": this.resolution[12], "scale": this.scale[12] },

  39. { "level": 13, "resolution": this.resolution[13], "scale": this.scale[13] },

  40. { "level": 14, "resolution": this.resolution[14], "scale": this.scale[14] },

  41. { "level": 15, "resolution": this.resolution[15], "scale": this.scale[15] },

  42. { "level": 16, "resolution": this.resolution[16], "scale": this.scale[16] },

  43. { "level": 17, "resolution": this.resolution[17], "scale": this.scale[17] },

  44. { "level": 18, "resolution": this.resolution[18], "scale": this.scale[18] },

  45. { "level": 19, "resolution": this.resolution[19], "scale": this.scale[19] }

  46. ]

  47. });

  48. this.loaded = true;

  49. this.onLoad(this);

  50. },

  51. getTileUrl: function (level, row, col) {

  52. var zoom = level - 1;

  53. var offsetX = parseInt(Math.pow(2, zoom));

  54. var offsetY = offsetX - 1;

  55. var numX = col - offsetX, numY = (-row) + offsetY ;

  56. var num = (col + row) % 8 + 1;

  57. return "http://online" + num + ".map.bdimg.com/tile/?qt=tile&x="+numX+"&y="+numY+"&z="+level+"&styles=sl&udt=20180505";

  58. }

  59. });

  60. });

BDVecLayer.js

  1. define(["dojo/_base/declare",

  2. "esri/layers/TiledMapServiceLayer",

  3. "esri/geometry/Extent",

  4. "esri/SpatialReference",

  5. "esri/layers/TileInfo"

  6. ], function (declare, TiledMapServiceLayer, Extent, SpatialReference, TileInfo) {

  7. return declare(TiledMapServiceLayer, {

  8. constructor: function () {

  9. this.spatialReference = new SpatialReference({ wkid: 102100 });

  10. this.initialExtent = (this.fullExtent = new Extent(-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892, this.spatialReference));

  11. this.scale = [591657527.591555,295828763.795777,147914381.897889,73957190.948944,36978595.474472,18489297.737236,9244648.868618,

  12. 4622324.434309,2311162.217155,1155581.108577,577790.554289,288895.277144,144447.638572,72223.819286,

  13. 36111.9096437,18055.9548224,9027.977411,4513.988705,2256.994353,1128.497176];

  14. this.resolution = [156543.033928,78271.5169639999,39135.7584820001,19567.8792409999,9783.93962049996,4891.96981024998,2445.98490512499,

  15. 1222.99245256249,611.49622628138,305.748113140558,152.874056570411,76.4370282850732,38.2185141425366,19.1092570712683,9.55462853563415,

  16. 4.77731426794937,2.38865713397468,1.19432856685505,0.597164283559817,0.298582141647617];

  17. this.tileInfo = new TileInfo({

  18. "rows": 256,

  19. "cols": 256,

  20. "compressionQuality": 90,

  21. "origin": {

  22. "x": -20037508.3427892,

  23. "y": 20037508.3427892

  24. },

  25. "spatialReference": this.spatialReference,

  26. "lods": [{ "level": 0, "resolution": this.resolution[0], "scale": this.scale[0] },

  27. { "level": 1, "resolution": this.resolution[1], "scale": this.scale[1] },

  28. { "level": 2, "resolution": this.resolution[2], "scale": this.scale[2] },

  29. { "level": 3, "resolution": this.resolution[3], "scale": this.scale[3] },

  30. { "level": 4, "resolution": this.resolution[4], "scale": this.scale[4] },

  31. { "level": 5, "resolution": this.resolution[5], "scale": this.scale[5] },

  32. { "level": 6, "resolution": this.resolution[6], "scale": this.scale[6] },

  33. { "level": 7, "resolution": this.resolution[7], "scale": this.scale[7] },

  34. { "level": 8, "resolution": this.resolution[8], "scale": this.scale[8] },

  35. { "level": 9, "resolution": this.resolution[9], "scale": this.scale[9] },

  36. { "level": 10, "resolution": this.resolution[10], "scale": this.scale[10] },

  37. { "level": 11, "resolution": this.resolution[11], "scale": this.scale[11] },

  38. { "level": 12, "resolution": this.resolution[12], "scale": this.scale[12] },

  39. { "level": 13, "resolution": this.resolution[13], "scale": this.scale[13] },

  40. { "level": 14, "resolution": this.resolution[14], "scale": this.scale[14] },

  41. { "level": 15, "resolution": this.resolution[15], "scale": this.scale[15] },

  42. { "level": 16, "resolution": this.resolution[16], "scale": this.scale[16] },

  43. { "level": 17, "resolution": this.resolution[17], "scale": this.scale[17] },

  44. { "level": 18, "resolution": this.resolution[18], "scale": this.scale[18] },

  45. { "level": 19, "resolution": this.resolution[19], "scale": this.scale[19] }

  46. ]

  47. });

  48. this.loaded = true;

  49. this.onLoad(this);

  50. },

  51. getTileUrl: function (level, row, col) {

  52. var zoom = level - 1;

  53. var offsetX = parseInt(Math.pow(2, zoom));

  54. var offsetY = offsetX - 1;

  55. var numX = col - offsetX, numY = (-row) + offsetY ;

  56. var num = (col + row) % 8 + 1;

  57. return "http://online" + num + ".map.bdimg.com/tile/?qt=tile&x="+numX+"&y="+numY+"&z="+level+"&styles=pl&scaler=1&udt=20180505";

  58. }

  59. });

  60. });

BDImgLayer.js

  1. define(["dojo/_base/declare",

  2. "esri/layers/TiledMapServiceLayer",

  3. "esri/geometry/Extent",

  4. "esri/SpatialReference",

  5. "esri/layers/TileInfo"

  6. ], function (declare, TiledMapServiceLayer, Extent, SpatialReference, TileInfo) {

  7. return declare(TiledMapServiceLayer, {

  8. constructor: function () {

  9. this.spatialReference = new SpatialReference({ wkid: 102100 });

  10. this.initialExtent = (this.fullExtent = new Extent(-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892, this.spatialReference));

  11. this.scale = [591657527.591555,295828763.795777,147914381.897889,73957190.948944,36978595.474472,18489297.737236,9244648.868618,

  12. 4622324.434309,2311162.217155,1155581.108577,577790.554289,288895.277144,144447.638572,72223.819286,

  13. 36111.9096437,18055.9548224,9027.977411,4513.988705,2256.994353,1128.497176];

  14. this.resolution = [156543.033928,78271.5169639999,39135.7584820001,19567.8792409999,9783.93962049996,4891.96981024998,2445.98490512499,

  15. 1222.99245256249,611.49622628138,305.748113140558,152.874056570411,76.4370282850732,38.2185141425366,19.1092570712683,9.55462853563415,

  16. 4.77731426794937,2.38865713397468,1.19432856685505,0.597164283559817,0.298582141647617];

  17. this.tileInfo = new TileInfo({

  18. "rows": 256,

  19. "cols": 256,

  20. "compressionQuality": 90,

  21. "origin": {

  22. "x": -20037508.3427892,

  23. "y": 20037508.3427892

  24. },

  25. "spatialReference": this.spatialReference,

  26. "lods": [{ "level": 0, "resolution": this.resolution[0], "scale": this.scale[0] },

  27. { "level": 1, "resolution": this.resolution[1], "scale": this.scale[1] },

  28. { "level": 2, "resolution": this.resolution[2], "scale": this.scale[2] },

  29. { "level": 3, "resolution": this.resolution[3], "scale": this.scale[3] },

  30. { "level": 4, "resolution": this.resolution[4], "scale": this.scale[4] },

  31. { "level": 5, "resolution": this.resolution[5], "scale": this.scale[5] },

  32. { "level": 6, "resolution": this.resolution[6], "scale": this.scale[6] },

  33. { "level": 7, "resolution": this.resolution[7], "scale": this.scale[7] },

  34. { "level": 8, "resolution": this.resolution[8], "scale": this.scale[8] },

  35. { "level": 9, "resolution": this.resolution[9], "scale": this.scale[9] },

  36. { "level": 10, "resolution": this.resolution[10], "scale": this.scale[10] },

  37. { "level": 11, "resolution": this.resolution[11], "scale": this.scale[11] },

  38. { "level": 12, "resolution": this.resolution[12], "scale": this.scale[12] },

  39. { "level": 13, "resolution": this.resolution[13], "scale": this.scale[13] },

  40. { "level": 14, "resolution": this.resolution[14], "scale": this.scale[14] },

  41. { "level": 15, "resolution": this.resolution[15], "scale": this.scale[15] },

  42. { "level": 16, "resolution": this.resolution[16], "scale": this.scale[16] },

  43. { "level": 17, "resolution": this.resolution[17], "scale": this.scale[17] },

  44. { "level": 18, "resolution": this.resolution[18], "scale": this.scale[18] },

  45. { "level": 19, "resolution": this.resolution[19], "scale": this.scale[19] }

  46. ]

  47. });

  48. this.loaded = true;

  49. this.onLoad(this);

  50. },

  51. getTileUrl: function (level, row, col) {

  52. var zoom = level - 1;

  53. var offsetX = parseInt(Math.pow(2, zoom));

  54. var offsetY = offsetX - 1;

  55. var numX = col - offsetX, numY = (-row) + offsetY ;

  56. var num = (col + row) % 8 + 1;

  57. return "http://shangetu" + num + ".map.bdimg.com/it/u=x="+numX+";y="+numY+";z="+level+";v=009;type=sate&fm=46&udt=20180505";

  58. }

  59. });

  60. });

从上面的getTileUrl 可以看到,三者返回的URL的地址是没有有区别的,在上述的URL有可能失效,为了得到最新的地址,学习了那篇文章,我做了如下工作:

1,先用百度地图JS API调用并显示百度地图,代码如下:

  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

  6. <style type="text/css">

  7. body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}

  8. </style>

  9. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=NkuwFBPlXG1uqPDsB9nK2nqpyLFF0SFV"></script>

  10. <title>地图展示</title>

  11. </head>

  12. <body>

  13. <div id="allmap"></div>

  14. </body>

  15. </html>

  16. <script type="text/javascript">

  17. // 百度地图API功能

  18. var map = new BMap.Map("allmap"); // 创建Map实例

  19. map.centerAndZoom(new BMap.Point(116.404, 39.915), 5); // 初始化地图,设置中心点坐标和地图级别

  20. map.addControl(new BMap.MapTypeControl()); //添加地图类型控件

  21. map.setCurrentCity("北京"); // 设置地图显示的城市 此项是必须设置的

  22. map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放

  23. </script>

你的密钥需要更改为百度API的密钥,有需要的要申请。

2,然后用chrome打开这个html,F12调试--NetWork--左边找一张切片,右边切换至预览面板


                                                                BDVecLayer.js(地图网址)


                                                                                BDImgLayer.js(影像链接)


                                                                                   BDAnoLayer.js(poi网址

只需要将最后面的数字替换掉就行了,20180505。

3.将上面的三个JS调用到地图主页。

代码如下:

  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  5. <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">

  6. <title>Baidu Map</title>

  7. <link rel="stylesheet" href="https://js./3.23/dijit/themes/tundra/tundra.css"/>

  8. <link rel="stylesheet" href="https://js./3.23/esri/css/esri.css">

  9. <style>

  10. html, body, #map {

  11. height: 100%;

  12. margin: 0;

  13. padding: 0;

  14. }

  15. .base-map-ano{

  16. position: absolute;

  17. right: 0pt;

  18. top:18pt;

  19. background: #e6edf1;

  20. border: #96aed1 1px solid;

  21. padding: 4px 5px;

  22. padding-left: 0px;

  23. padding-top: 0px;

  24. display: none;

  25. font-weight: normal;

  26. }

  27. .base-map{

  28. position: absolute;

  29. right: 15pt;

  30. top:15pt;

  31. background: #f0f0f0;

  32. border: #96aed1 1px solid;

  33. width: auto;

  34. height: auto;

  35. z-index: 99;

  36. font:normal 11px "宋体",Arial;

  37. color:#868686;

  38. }

  39. .base-map-switch{

  40. padding: 4px 8px;

  41. float: left;

  42. }

  43. .base-map-switch-active{

  44. background:#e6edf1;

  45. font-weight: bold;

  46. color: #4d4d4d;

  47. }

  48. .base-map-switch:hover{

  49. cursor: pointer;

  50. }

  51. .base-map-switch-center{

  52. border: 1px #96aed1 solid;

  53. border-top:none;

  54. border-bottom:none;

  55. }

  56. </style>

  57. <script type="text/javascript">

  58. dojoConfig = {

  59. parseOnLoad: true,

  60. packages: [{

  61. name: 'bdlib',

  62. location: this.location.pathname.replace(/\/[^/]+$/, "")+"/js"

  63. }]

  64. };

  65. </script>

  66. <script src="https://js./3.23/"></script>

  67. <script src="js/jquery-1.8.3.js"></script>

  68. <script>

  69. var map,showMap,anoCtrl;

  70. require(["esri/map",

  71. "bdlib/BDVecLayer",

  72. "bdlib/BDImgLayer",

  73. "bdlib/BDAnoLayer",

  74. "esri/layers/FeatureLayer",

  75. "esri/geometry/Point",

  76. "esri/SpatialReference",

  77. "dojo/domReady!"],

  78. function (Map,

  79. BDVecLayer,

  80. BDImgLayer,

  81. BDAnoLayer,

  82. FeatureLayer,

  83. Point,

  84. SpatialReference

  85. ){

  86. map = new Map("map", {

  87. logo: false

  88. });

  89. var vecMap = new BDVecLayer();

  90. var imgMap = new BDImgLayer();

  91. var anoMap = new BDAnoLayer();

  92. map.addLayer(vecMap);

  93. map.addLayers([imgMap,anoMap]);

  94. imgMap.hide();

  95. anoMap.hide();


  96. var pt = new Point(7038512.810510807, 2629489.7975553474, new SpatialReference({ wkid: 102100 }));

  97. map.centerAndZoom(pt, 5);


  98. showMap = function(layer){

  99. //设置按钮样式

  100. var baseMap = ["vec","img"];

  101. for(var i= 0, dl=baseMap.length;i<dl;i++){

  102. $("#"+baseMap[i]).removeClass("base-map-switch-active");

  103. }

  104. $("#"+layer).addClass("base-map-switch-active");

  105. //设置显示地图

  106. switch(layer){

  107. case "img":{//影像

  108. vecMap.hide();

  109. imgMap.show();

  110. $("#ano").show();

  111. break;

  112. }

  113. default :{//地图

  114. vecMap.show();

  115. imgMap.hide();

  116. anoMap.hide();

  117. $("#ano").hide();

  118. $("#chkAno").attr("checked",false);

  119. break;

  120. }

  121. }

  122. };

  123. anoCtrl = function(){

  124. if($("#chkAno").prop("checked")){

  125. anoMap.show();

  126. }

  127. else{

  128. anoMap.hide();

  129. }

  130. }

  131. });

  132. </script>

  133. </head>

  134. <body>

  135. <div id="map">

  136. <div class="base-map">

  137. <div id="vec" class="base-map-switch base-map-switch-active" onclick="showMap('vec')">地图</div>

  138. <div id="img" class="base-map-switch base-map-switch-center" onclick="showMap('img')">影像

  139. <div id="ano" class="base-map-ano">

  140. <input id="chkAno" type="checkbox" name="chkAno" value="chkAno" onchange="anoCtrl()"/>标注

  141. </div>

  142. </div>

  143. </div>

  144. </div>

  145. </body>

  146. </html>


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多