加载离线地图总结
部署好离线arcgis for js API,发布地图服务后,下一步就是通过编程显示地图,我刚开始使用的例子是:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Intro to MapView - Create a 2D map - 3.17</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<link rel="stylesheet" href="http://localhost/arcgis_js_api/library/3.17/esri/css/esri.css">
<script src="http://localhost/arcgis_js_api/library/3.17/dojo/dojo.js"></script>
<script>
require(["esri/map","esri/layers/ArcGISDynamicMapServiceLayer",
"dojo/domReady!"],
function (Map, ArcGISDynamicMapServiceLayer) {
var map = new Map("mapDiv");
//利用url创建一个动态地图服务对象
var layer = new ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/jiyuan/jiyuan/MapServer");
//将地图服务对象添加到地图容器中
map.addLayer(layer);
})
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>
运行一下,熬了好几个夜晚,本想一键搞定,却还是出现错误,快崩溃了,没办法,撸起袖子再继续干吧,也不能随便放弃啊。
仔细查看代码,登录arcgis官方SDK查询,终于发现问题,原来上述例子中用的esri/layers/ArcGISDynamicMapServiceLayer类在最新版本的API 4.7中根本不存在,下载旧版本3.17看是有的,请教了网友得知最新版的API可能没把旧版的一些功能移植过来。
那就重新部署3,17版本的,可运行还是出错!!!
再查看官方SDK,里面有一些简单的例子,复制过来再运行一下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
<title>Test Map</title>
<link rel="stylesheet" href="http://localhost/arcgis_js_api/library/4.7/dijit/themes/claro/claro.css" />
<link rel="stylesheet" href="http://localhost/arcgis_js_api/library/4.7/esri/css/main.css" />
<style>
html,
body,
#viewDiv {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
</style>
<script src="http://localhost/arcgis_js_api/library/4.7/dojo/dojo.js"></script>
<script>
var myMap, view;
require([
"esri/Basemap",
"esri/layers/TileLayer",
"esri/Map",
"esri/views/MapView",
"dojo/domReady!"
], function (Basemap, TileLayer, Map, MapView) {
// --------------------------------------------------------------------
// If you do not have public Internet access then use the Basemap class
// and point this URL to your own locally accessible cached service.
//
// Otherwise you can just use one of the named hosted ArcGIS services.
// https://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer
// --------------------------------------------------------------------
var layer = new TileLayer({
url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"
});
var customBasemap = new Basemap({
baseLayers: [layer],
title: "Custom Basemap",
id: "myBasemap"
});
myMap = new Map({
basemap: customBasemap
});
view = new MapView({
center: [120.87, 34.57], // long, lat
container: "viewDiv",
map: myMap,
zoom: 3
});
});
</script>
</head>
<body class="claro">
<div id="viewDiv"></div>
</body>
</html>
这次运行成功了,说明离线API部署没问题,换成我自己发布的地图服务,
https://localhost:6443/arcgis/rest/services/MyMapService/MapServer
运行出来又又又出错了,马上要崩溃了,再坚持一下吧,继续查找问题,肯定是地图服务的问题,打开官方例子的地图服务地址:
https://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer
再打开我的,进行对比,发现,前者有Tile Info信息,而我的没有,可能是我发布的地图服务格式不对
经过网上查询得知,有Tile Info信息的是具有缓存功能的地图服务,那就重新发布地图服务吧,参考了
https://blog.csdn.net/m0_37044606/article/details/73294416,
然后运行加载我自己地图服务的程序,运行成功!!!!!!