| | |
| | | const locationIMGPath = 'img/location.png'; |
| | | const markerBalckPath = 'img/marker_black.svg'; |
| | | const markerRedPath = 'img/marker_red.svg'; |
| | | |
| | | const markerBluePath = 'img/marker_blue.svg'; |
| | | let map; |
| | | let lastMarker = null; |
| | | let lastClickedMarker = null; |
| | | let isShowWaterIntakeDetail = false; |
| | | let zoom=12; |
| | | let zoom = 12; |
| | | |
| | | |
| | | document.addEventListener('DOMContentLoaded', function () { |
| | |
| | | mountMethodToWindow(); |
| | | |
| | | }); |
| | | window.onload = function() { |
| | | //加载坐标点 |
| | | window.onload = function () { |
| | | //加载坐标点 |
| | | window.Android.loadMarker(); |
| | | }; |
| | | // 初始化地图 |
| | | function initMap() { |
| | | // 影像底图 |
| | | var imageURL = "http://t0.tianditu.gov.cn/img_w/wmts?" + |
| | | "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" + |
| | | "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=d8beed89b43160a9a185e5aff431f85d"; |
| | | //创建自定义图层对象 |
| | | var lay = new T.TileLayer(imageURL, { minZoom: 1, maxZoom: 18 }); |
| | | var config = { layers: [lay] }; |
| | | //影像注记(地图上的地址) |
| | | var imgURL2 ="http://t0.tianditu.gov.cn/cia_w/wmts?" +"SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" + |
| | | "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}" + |
| | | "&tk=d8beed89b43160a9a185e5aff431f85d"; |
| | | var lay = new T.TileLayer(imageURL, { minZoom: 6, maxZoom: 18 }); |
| | | var lay2 = new T.TileLayer(imgURL2, { minZoom: 6, maxZoom: 18 }); |
| | | var config = { layers: [lay,lay2] }; |
| | | map = new T.Map("mapDiv", config); |
| | | var scale = new T.Control.Scale(); |
| | | //添加比例尺控件 |
| | | map.addControl(scale); |
| | | map.centerAndZoom(new T.LngLat(116.40769, 39.89945), zoom); //地图的初始化中心点,此为北京的经纬度 |
| | | map.addEventListener("click", closeWaterIntakeDetail); |
| | | } |
| | |
| | | |
| | | for (let i = 0; i < data_info.length; i++) { |
| | | let icon = new T.Icon({ |
| | | iconUrl: markerBalckPath, |
| | | iconUrl: markerBluePath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | }); |
| | |
| | | |
| | | // 手机获取到定位后显示定位 |
| | | function locationOverLay(lng, lag) { |
| | | map.centerAndZoom(new T.LngLat(lng, lag), 18); |
| | | console.log("function》》》》》locationOverLay"); |
| | | map.centerAndZoom(new T.LngLat(lng, lag), map.getZoom()); |
| | | |
| | | let icon = new T.Icon({ |
| | | iconUrl: locationIMGPath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | }); |
| | | |
| | | let marker = new T.Marker(new T.LngLat(lng, lag), { icon: icon }); |
| | | map.addOverLay(marker); |
| | | } |
| | | |
| | | |
| | | //设置地图中心点 |
| | | function setCenterAndZoom(lng, lat,thiszoom) { |
| | | zoom=thiszoom; |
| | | console.log("function》》》》》setCenterAndZoom>>>>lng:" + lng + ",lat:" + lat); |
| | | map.centerAndZoom(new T.LngLat(lng, lat), zoom); |
| | | } |
| | | |
| | | |
| | | // 将方法挂载到 window 上 |
| | | function mountMethodToWindow() { |
| | | window.locationOverLay = locationOverLay; |
| | | window.showToast = showToast; |
| | | window.addMarker = addMarker; |
| | | window.setCenterAndZoom = setCenterAndZoom; |
| | | window.updateLocation=updateLocation; |
| | | } |
| | | |
| | | // 调用原生安卓方法显示取水口详情 |
| | |
| | | |
| | | // 调用原生安卓方法隐藏取水口详情 |
| | | function closeWaterIntakeDetail(data) { |
| | | if (lastClickedMarker!== null) { |
| | | if (lastClickedMarker !== null) { |
| | | // 如果有点击取水口则将取水口恢复成黑色 |
| | | // 上次点击的标注改为黑色 |
| | | var defaulticon = new T.Icon({ |
| | | iconUrl: markerBalckPath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | iconUrl: markerBluePath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | }); |
| | | lastClickedMarker.setIcon(defaulticon); |
| | | } |
| | | if (isShowWaterIntakeDetail) { |
| | | } |
| | | if (isShowWaterIntakeDetail) { |
| | | // 假如显示了取水口详情则隐藏取水口详情 |
| | | isShowWaterIntakeDetail = false; |
| | | window.Android.closeWaterIntakeView(); |
| | | } else { |
| | | } else { |
| | | // 假如没有显示取水口则添加点击的坐标 |
| | | getLngLat(data.lnglat); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 点击标注的事件 |
| | |
| | | |
| | | // 修改点击标注的图标 |
| | | function chageMarkerIcon(data) { |
| | | // 点击的标注改为红色 |
| | | var currentMarker = data.target; |
| | | // 点击后图标 |
| | | var clickedIcon = new T.Icon({ |
| | | iconUrl: markerRedPath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | }); |
| | | // 设置当前点击的 marker 为点击后图标 |
| | | currentMarker.setIcon(clickedIcon); |
| | | // 判断点击的不是同一个坐标 |
| | | if (lastClickedMarker!== null) { |
| | | if (!isEqualsLngLat(data.target.getLngLat(), lastClickedMarker.getLngLat())) { |
| | | var defaulticon = new T.Icon({ |
| | | iconUrl: markerBalckPath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | }); |
| | | lastClickedMarker.setIcon(defaulticon); |
| | | } |
| | | } |
| | | lastClickedMarker = data.target; |
| | | // 点击的标注改为红色 |
| | | var currentMarker = data.target; |
| | | // 点击后图标 |
| | | var clickedIcon = new T.Icon({ |
| | | iconUrl: markerRedPath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | }); |
| | | // 设置当前点击的 marker 为点击后图标 |
| | | currentMarker.setIcon(clickedIcon); |
| | | // 判断点击的不是同一个坐标 |
| | | if (lastClickedMarker !== null) { |
| | | if (!isEqualsLngLat(data.target.getLngLat(), lastClickedMarker.getLngLat())) { |
| | | var defaulticon = new T.Icon({ |
| | | iconUrl: markerBluePath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | }); |
| | | lastClickedMarker.setIcon(defaulticon); |
| | | } |
| | | } |
| | | lastClickedMarker = data.target; |
| | | } |
| | | //调安卓原生 |
| | | function showToast() { |
| | |
| | | console.log("function》》》》》addMarker"); |
| | | var array = JSON.parse(jsonData); |
| | | var icon = new T.Icon({ |
| | | iconUrl: markerBalckPath, |
| | | iconUrl: markerBluePath, |
| | | iconSize: new T.Point(27, 27), |
| | | iconAnchor: new T.Point(10, 25) |
| | | }); |
| | |
| | | addMarkerListener(data) |
| | | }); |
| | | map.addOverLay(marker); // 将标注添加到地图中 |
| | | let label = new T.Label({ |
| | | text: element[2], |
| | | position: marker.getLngLat(), |
| | | offset: new T.Point(-35, 8), // 设置标注文字的位置 |
| | | opacity: 1, // 设置文本的显示不透明度(范围0-1) |
| | | }); |
| | | label.setBorderLine(0); // 设置文本的边框线宽 |
| | | label.setBackgroundColor("transparent"); // 设置文本的背景色(透明色) |
| | | label.setFontColor("#0000FF"); |
| | | label.setFontSize(10); |
| | | map.addOverLay(label); |
| | | }); |
| | | return "addMarker加载成功" |
| | | } |
| | | //保存定位坐标生成轨迹 |
| | | var path=[]; |
| | | var lineLayer = new T.Polyline([], { color: 'red', weight: 3, opacity: 0.8 }); |
| | | |
| | | function updateLocation(log,lat){ |
| | | var lastLat=lat; |
| | | const intervalId = setInterval(() => { |
| | | lastLat=lastLat+0.0001; |
| | | var newPoint = new T.LngLat(log,lastLat); |
| | | path.push(newPoint); |
| | | lineLayer.setLngLats(path); |
| | | map.addOverLay(lineLayer); |
| | | // 移动地图视角到最后一个位置 |
| | | map.panTo(newPoint); |
| | | }, 500); |
| | | |
| | | } |
| | | |
| | | |
| | | })(); |
| | | |