1. 程式人生 > >騰訊地圖經緯度選擇和圖層功能

騰訊地圖經緯度選擇和圖層功能

$(function () { //其他頁面跳轉攜帶經緯度引數,則定位到該經緯度,否則定位到當前位置 var lngLat = "${param.lngLat}"; if (lngLat != undefined && lngLat != '') { try { var arr = lngLat.split(","); var lng = arr[0]; var lat = arr[1]; init
(lng,lat); } catch (e) { init(); } } else { init(); } bindEvent(); }); var map; var marker; var center; function init(lng,lat) { //定義地圖的中心地理座標 if (lng != undefined && lng !='' &&
lat != undefined && lat != '') { center = new qq.maps.LatLng(parseFloat(lat),parseFloat(lng)); } else { center = new qq.maps.LatLng(36.726032,114.102802); //該方法為非同步 getLocation(); } //初始化地圖 initMap(lng,lat); //建立矩形疊加層,圖片url請修改
createGroundOverlay(); //新增折線覆蓋物 createPolygon(); } //初始化地圖 function initMap() { //定義map變數 呼叫 qq.maps.Map() 建構函式 獲取地圖顯示容器 map = new qq.maps.Map(document.getElementById("allMap"),{ center : center, // 地圖的中心地理座標 zoom : 15, // 預設縮放級別,縮放級別4-17 mapTypeId : qq.maps.MapTypeId.HYBRID, // 衛星地圖模式 mapTypeControl:false, }); //建立一個Marker marker = new qq.maps.Marker({ //設定Marker的位置座標 position: center, //設定顯示Marker的地圖 map: map, draggable:true }); setLngLat(center.getLng(),center.getLat()); } //繫結事件 function bindEvent() { //新增監聽事件 獲取滑鼠單擊事件,新增標記點 qq.maps.event.addListener(map, 'click', function(event) { marker.setMap(null); marker=new qq.maps.Marker({ position:event.latLng, map:map, draggable:true }); setLngLat(event.latLng.getLng(),event.latLng.getLat()); }); } //獲取當前位置 function getLocation(){ var geolocation = new qq.maps.Geolocation("6IWBZ-NGJKP-UT5DR-V7TJL-5U7S2-K4BW4", "yutao"); geolocation.getLocation( function (position) { var lat = position.lat; var lng = position.lng; center = new qq.maps.LatLng(lat,lng); map.setCenter(center); marker.setMap(null); marker=new qq.maps.Marker({ position:center, map:map, draggable:true }); setLngLat(lng,lat); }, function () { center = new qq.maps.LatLng(36.726032,114.102802); }, {timeout: 8000}); } //建立矩形疊加層,圖片url請修改 function createGroundOverlay(){ new qq.maps.GroundOverlay({ map:map, zIndex:-1, imageUrl:Global.contextPath + '/static/pages/monitor/img/yh.png', bounds:new qq.maps.LatLngBounds(new qq.maps.LatLng(36.7399126700,114.0859880600), new qq.maps.LatLng(36.7210522200,114.1175883900)) }); new qq.maps.GroundOverlay({ map:map, zIndex:-1, imageUrl:Global.contextPath + '/static/pages/monitor/img/zs1.png', bounds:new qq.maps.LatLngBounds(new qq.maps.LatLng(36.7348870300,114.0916279100), new qq.maps.LatLng(36.6876175900,114.1322266000)) }); new qq.maps.GroundOverlay({ map:map, zIndex:-1, imageUrl:Global.contextPath + '/static/pages/monitor/img/py.png', bounds:new qq.maps.LatLngBounds(new qq.maps.LatLng(36.7434735600,113.8977854400), new qq.maps.LatLng(36.6955655900,113.9444176500)) }); new qq.maps.GroundOverlay({ map:map, zIndex:-1, imageUrl:Global.contextPath + '/static/pages/monitor/img/zs3.png', bounds:new qq.maps.LatLngBounds(new qq.maps.LatLng(36.6363133800,114.1114188800), new qq.maps.LatLng(36.5997751400,114.1538538600)) }); } // 新增折線覆蓋物 function createPolygon(){ var polygon = new qq.maps.Polygon({ path: [ new qq.maps.LatLng(36.7362187706,114.0991544724), new qq.maps.LatLng(36.7389529065,114.1000986099), new qq.maps.LatLng(36.7378351898,114.1064715385), new qq.maps.LatLng(36.7372333356,114.1064715385), new qq.maps.LatLng(36.7372333356,114.1064715385), new qq.maps.LatLng(36.7367518488,114.1100120544), new qq.maps.LatLng(36.7345507280,114.1096687317), new qq.maps.LatLng(36.7336565046,114.1133165359), new qq.maps.LatLng(36.7231829982,114.1097760201), new qq.maps.LatLng(36.7265023429,114.1034245491), new qq.maps.LatLng(36.7282565500,114.1022658348), new qq.maps.LatLng(36.7283941332,114.1003775597), new qq.maps.LatLng(36.7274654419,114.1000986099), new qq.maps.LatLng(36.7275342343,114.0981030464), new qq.maps.LatLng(36.7280157788,114.0966224670), new qq.maps.LatLng(36.7266915240,114.0947556496), new qq.maps.LatLng(36.7278094030,114.0920305252), new qq.maps.LatLng(36.7322119680,114.0912365913), new qq.maps.LatLng(36.7322119680,114.0912365913), new qq.maps.LatLng(36.7356512963,114.0954422951), new qq.maps.LatLng(36.7364767122,114.0961503983), new qq.maps.LatLng(36.7360124168,114.0991115570) ], strokeColor: '#ff0000', strokeWeight: 1, fillColor: new qq.maps.Color(220,220,220,0.3), map: map }); } //設定經緯度 function setLngLat(lng,lat) { $("#lng").val(lng); $("#lat").val(lat); }