<!DOCTYPE html>
|
<html>
|
<head>
|
<meta charset="utf-8">
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
|
<title>消火栓导入预览</title>
|
<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
|
<link href="../css/public.css" rel="stylesheet" />
|
<script src="http://cache.amap.com/lbs/static/es5.min.js"></script>
|
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=3627ed9deaac2622e26a7169f0c36b1b&plugin=AMap.Geocoder"></script>
|
<script src="../js/jquery-2.1.1.min.js"></script>
|
<script src="../js/public.js"></script>
|
<script src="../js/tools.js"></script>
|
<script src="../js/base64.js"></script>
|
</head>
|
|
<body>
|
|
<div id="container"></div>
|
|
<div class="search-box" id="search_box" >
|
<input class="search-text" id="search_text" type="text" placeholder="请输入详细地址" onkeypress="keySearchAddress()" />
|
<input class="search-button" id="search_button" type="button" onclick="searchAddress()" />
|
</div>
|
|
<div class=" info-box" id="info_box">
|
信息提示
|
<hr>
|
<div id="info_text">
|
</div>
|
</div>
|
|
<script>
|
|
let map;//地图对象 使用 ToolBar时,地图对象需要定义位map 否则会报错
|
let _map;//地图对象
|
let _layer;//图层对象
|
let _allSignObjs = [];//所有标记对象(消火栓)
|
let _selectedOverlay = null;//选择的覆盖物(消火栓)
|
|
$(document).ready(function () {
|
|
try {
|
map = _map = new AMap.Map('container', {
|
resizeEnable: true,
|
expandZoomRange: true,
|
zoom: 10,
|
zooms: [3, 20]
|
});
|
|
//地图点击时取消选择
|
_map.on('click', function (e) {
|
cancelSelectSignObj();
|
});
|
|
//地图点击时关闭搜索面板
|
_map.on("click", function () {
|
if ($("#search_text").css("display") == 'block') {
|
$("#search_text").hide();
|
}
|
});
|
|
//地图加载完成事件
|
_map.on("complete", function () {//地图加载完成
|
_layer = new AMap.LabelsLayer({
|
zooms: [3, 20],
|
zIndex: 1000,
|
// 该层内标注是否避让
|
collision: false,
|
});
|
_map.add(_layer);
|
callbackObj.loadCompleted();
|
})
|
|
}
|
catch (e) {
|
callbackObj.loadFailed();
|
}
|
})
|
|
//设置标记对象
|
function setSignObjs(signObjs) {
|
if (_allSignObjs.length > 0) {
|
_layer.clear();
|
}
|
if (isEmpty(signObjs)) {
|
_allSignObjs = [];
|
}
|
else {
|
_allSignObjs = signObjs;
|
}
|
|
|
let icon = {
|
// 图标类型,现阶段只支持 image 类型
|
type: 'image',
|
// 图片 url
|
image: sign_base64.point_r,
|
// 图片尺寸
|
size: [16, 16],
|
// 图片相对 position 的锚点,默认为 bottom-center
|
anchor: 'center',
|
};
|
let labelMarkers = [];
|
_allSignObjs.forEach(function (x) {
|
let labelMarker = new AMap.LabelMarker({
|
//name: x.SignId, // 此属性非绘制文字内容,仅作为标识使用
|
position: [x.Point.X, x.Point.Y],
|
//zIndex: 1,
|
// 将第一步创建的 icon 对象传给 icon 属性
|
icon: icon,
|
// 将第二步创建的 text 对象传给 text 属性
|
//text: text,
|
});
|
labelMarker.objInfo = x;
|
labelMarker.on("click", function (e) {
|
setSelectStyle(e.target);
|
callbackObj.selectSignObj(e.target.objInfo.SignId);
|
});
|
labelMarkers.push(labelMarker);
|
});
|
if (labelMarkers.length > 0) {
|
_layer.add(labelMarkers);
|
}
|
cancelSelectSignObj();
|
setInfoText();
|
return true;
|
}
|
|
//设置取消选择样式
|
function setCancelSelectStyle() {
|
if (_selectedOverlay) {
|
_map.clearInfoWindow();
|
_selectedOverlay = null;
|
}
|
}
|
|
//取消选择标记对象
|
function cancelSelectSignObj() {
|
if (_selectedOverlay) {
|
setCancelSelectStyle();
|
callbackObj.selectSignObj("");
|
}
|
}
|
|
//设置选择样式
|
function setSelectStyle(overlay) {
|
setCancelSelectStyle();
|
let info = [];
|
info.push("<div className='input-card'>");
|
info.push("<label style=\"color: blue\">消火栓</label>");
|
info.push("<p class='input-item'>名称:");
|
info.push(overlay.objInfo.SignName);
|
info.push("</p>");
|
info.push("<p class='input-item'>经纬度:");
|
info.push(overlay.objInfo.Point.X + "," + overlay.objInfo.Point.Y);
|
info.push("</p>");
|
info.push("</div>");
|
infoWindow = new AMap.InfoWindow({
|
anchor: 'bottom-center',
|
content: info.join(""),
|
offset: new AMap.Pixel(0, -16)
|
});
|
|
infoWindow.open(_map, overlay.getPosition());
|
_selectedOverlay = overlay;
|
}
|
|
//选择标记对象
|
function selectSignObj(signId) {
|
if (isEmpty(signId)) {
|
return;
|
}
|
if (isEmpty(_allSignObjs)) {
|
_allSignObjs = [];
|
}
|
|
let signObj = _allSignObjs.find(function (x) { return x.SignId == signId; });
|
if (signObj) {
|
let allOverlays = _layer.getAllOverlays();
|
let overlay = allOverlays.find(function (x) { return x.objInfo && x.objInfo.SignId == signId; });
|
if (overlay) {
|
setSelectStyle(overlay);
|
callbackObj.selectSignObj(signId);
|
}
|
}
|
}
|
|
//设置信息文本
|
function setInfoText() {
|
if (isEmpty(_allSignObjs)) {
|
_allSignObjs = [];
|
}
|
let info = "消火栓:" + _allSignObjs.length;
|
document.getElementById('info_text').innerText = info;
|
}
|
|
//设置搜索面板的可见性
|
function setSearchPanelVisibility() {
|
document.getElementById('search_box').hidden = !document.getElementById('search_box').hidden;
|
}
|
|
//设置信息提示面板的可见性
|
function setInfoPanelVisibility() {
|
document.getElementById('info_box').hidden = !document.getElementById('info_box').hidden;
|
}
|
|
//根据详细地址查看地图
|
function locateByAddress(address) {
|
if (isEmpty(address)) {
|
return;
|
}
|
let geocoder = new AMap.Geocoder({});
|
//地理编码,返回地理编码结果
|
geocoder.getLocation(address, function (status, result) {
|
if (status === 'complete' && result.info === 'OK') {
|
let geocode = result.geocodes;
|
let lnglat = [geocode[0].location.getLng(), geocode[0].location.getLat()];
|
let marker = new AMap.Marker({ position: lnglat });
|
_map.setFitView(marker);
|
}
|
else {
|
let error = { ErrorType: error_type.locate_address, Message: "根据详细地址定位地图失败" };
|
callbackObj.handingError(JSON.stringify(error));
|
}
|
});
|
}
|
|
//搜索地址
|
function searchAddress() {
|
if ($("#search_text").css("display") == 'block') {
|
|
let address = $("#search_text").val();
|
if (!isEmpty(address)) {
|
|
locateByAddress(address);
|
}
|
$("#search_text").hide();
|
}
|
else {
|
$("#search_text").show();
|
}
|
}
|
|
//enter搜索地址
|
function keySearchAddress() {
|
|
if (event.keyCode == 13) {
|
searchAddress();
|
}
|
}
|
|
//根据点获取地址
|
getAddressByLnglat = function (lnglat, callback) {
|
let geocoder = new AMap.Geocoder({});
|
geocoder.getAddress(lnglat, function (status, result) {
|
if (status === 'complete' && result.info === 'OK') {
|
var address = result.regeocode.formattedAddress; //返回地址描述
|
callback(address);
|
}
|
else {
|
callback(null);
|
}
|
});
|
}
|
|
//地图模板样式
|
function setMapStyle(enName) {
|
_map.setMapStyle('amap://styles/' + enName);
|
}
|
|
|
|
</script>
|
</body>
|
|
</html>
|