You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.2 KiB
87 lines
3.2 KiB
2 years ago
|
// 高德地图工具类
|
||
|
var MapUtil = {
|
||
|
/**
|
||
|
* 添加地图边界
|
||
|
* <pre>
|
||
|
* config:
|
||
|
* region: 行政区划代码
|
||
|
* areaName: 行政区划名称
|
||
|
* earecode: admin.earecode
|
||
|
*
|
||
|
* </pre>
|
||
|
* @param {Object} map 高德地图 map 实例
|
||
|
* @param {{ region:string,
|
||
|
* areaName:string,
|
||
|
* earecode:string}} config 配置
|
||
|
*/
|
||
|
add_border: function (map, config) {
|
||
|
var district;
|
||
|
|
||
|
var areaDisLevel = config.earecode;
|
||
|
var len = areaDisLevel.length;
|
||
|
var areaName = '';
|
||
|
if (len == 6) {
|
||
|
areaDisLevel = 'district';
|
||
|
} else if (len == 4) {
|
||
|
areaDisLevel = 'city';
|
||
|
} else {
|
||
|
areaDisLevel = 'province';
|
||
|
areaName = '河北省';
|
||
|
config.areaName = areaName;
|
||
|
}
|
||
|
|
||
|
if (map != null) {
|
||
|
var obj = MAP_BORDER_ARR[config.region];
|
||
|
if (obj != null) {
|
||
|
var polygon = new AMap.Polygon({
|
||
|
path: obj.borderArray,
|
||
|
fillColor: '#80d8ff', // 多边形填充颜色
|
||
|
strokeWeight: 5, // 线条宽度,默认为 1
|
||
|
fillOpacity: 0,
|
||
|
strokeColor: '#0091ea', // 线条颜色
|
||
|
});
|
||
|
|
||
|
map.add(polygon);
|
||
|
if (obj.zoom) {
|
||
|
map.setZoom(10);
|
||
|
}
|
||
|
map.setCenter(obj.center);
|
||
|
} else {
|
||
|
var polygons = [];
|
||
|
var name = config.areaName;
|
||
|
AMap.service('AMap.DistrictSearch', function () {//回调函数
|
||
|
var opts = {
|
||
|
showbiz: false,
|
||
|
subdistrict: 0, //获取边界不需要返回下级行政区
|
||
|
extensions: 'all' //返回行政区边界坐标组等具体信息
|
||
|
};
|
||
|
district = new AMap.DistrictSearch(opts);
|
||
|
district.setLevel(areaDisLevel);//district、city、province、country
|
||
|
district.search(name, function (status, result) {
|
||
|
if (polygons == null || polygons.length == 0) {
|
||
|
map.remove(polygons); //清除上次结果
|
||
|
polygons = [];
|
||
|
}
|
||
|
var bounds = result.districtList[0].boundaries;
|
||
|
if (bounds) {
|
||
|
for (var i = 0, l = bounds.length; i < l; i++) {
|
||
|
//生成行政区划polygon
|
||
|
var polygon = new AMap.Polygon({
|
||
|
strokeWeight: 5,
|
||
|
path: bounds[i],
|
||
|
fillOpacity: 0,
|
||
|
fillColor: '#80d8ff',
|
||
|
strokeColor: '#0091ea'
|
||
|
});
|
||
|
polygons.push(polygon);
|
||
|
}
|
||
|
}
|
||
|
map.add(polygons);
|
||
|
map.setFitView(polygons);//视口自适应
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
};
|