高德地圖官方API:https://lbs.amap.com/api/javascript-api/reference/map
一、js中
/*初始化數據*/ function initData() { var list = []; for (var i = 0; i < 3; i++) { list.push({ number: 0 }); } $scope.numberList = list; list = []; for (var j = 0; j < 3; j++) { list.push({ day: "-", type: "-", temperature: "-" }); } $scope.weatherList = list; $scope.nowDate = "00:00"; $scope.ampm = "-"; /*每秒調用一次 refreshDate方法*/ window.setInterval(refreshDate, 1000); /*構建地圖對象*/ $scope.map = new AMap.Map('container', { zoom: 10, resizeEnable: true }); //InfoWindow 信息窗體 $scope.mapInfoWindow = new AMap.InfoWindow({ //Pixel 像素坐標,確定地圖上的一個像素點。 offset: new AMap.Pixel(0, -30) }); } /首頁數據*/ function refreshPageData(complete) { var params = { "t": 1 }; $http({ url: 'gas/home/queryData', method: 'POST', data: { params: params } }).then(function ($Data) { if ($Data.data.STATUS === true) { $scope.numberList = $Data.data.numberList; if ($Data.data.isSelectNumber === true) { $scope.selectClassName = "select"; } var stationList = $Data.data.stationList; $scope.stationList = { data: stationList, name: "stationName", id: "stationCode" }; if (stationList != null && stationList.length > 0) { $scope.stationCode = stationList[0]["stationCode"]; } complete && complete(); } else { } }); } /*獲取當前時間*/ function refreshDate() { var nowDate = new Date(); var h = nowDate.getHours(); $scope.$apply(function () { $scope.nowDate = zero(hours(h)) + "." + zero(nowDate.getMinutes()); $scope.ampm = h < 12 ? "AM" : "PM"; }); function hours(value) { return value % 12; } function zero(str) { str = "" + str; if (str.length === 1) { str = "0" + str; } return str; } } /*獲取本地城市地址*/ function refreshLocalCity(complete) { AMap.plugin('AMap.CitySearch', function () { new AMap.CitySearch().getLocalCity(function (status, result) { if (status === 'complete' && result.info === 'OK') { $scope.$apply(function () { $scope.city = result.city; }); complete && complete(); } }) }); } /*刷新地圖數據*/ function refreshMap() { $http({ url: 'gas/station/queryStationList', method: 'POST', data: { params: {} } }).then(function ($Data) { if ($Data.data.STATUS == true) { refreshMapPoint($Data.data.DATA); } else { } }); function refreshMapPoint(list) { for (var i = 0; i < list.length; i++) { var item = list[i]; addMarker(item); } $scope.map.setFitView(); } function addMarker(item) { var typeClassDic = { 1: "", 2: " tp2", 3: " tp3" }; var cxt = "<div class='marker-icon" + typeClassDic[item.type] + "'>"; cxt += "<svg class='icon' style='' viewBox='0 0 1024 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='32' height='32'><defs><style type='text/css'/></defs><path class='map-marker' d='M658.285714 365.714286q0-60.571429-42.857143-103.428571t-103.428571-42.857143-103.428571 42.857143-42.857143 103.428571 42.857143 103.428571 103.428571 42.857143 103.428571-42.857143 42.857143-103.428571zm146.285714 0q0 62.285714-18.857143 102.285714l-208 442.285714q-9.142857 18.857143-27.142857 29.714286t-38.571429 10.857143-38.571429-10.857143-26.571429-29.714286l-208.571429-442.285714q-18.857143-40-18.857143-102.285714 0-121.142857 85.714286-206.857143t206.857143-85.714286 206.857143 85.714286 85.714286 206.857143z' fill='#2c2c2c'/>"; cxt += "</div>"; //構造一個點標記對象,通過MarkerOptions設置點標記對象的屬性 var marker = new AMap.Marker({ position: new AMap.LngLat(item.stationLng, item.stationLat), title: item.stationName, content: cxt, offset: new AMap.Pixel(-16, -32), map: $scope.map }); marker.content = item; //點擊事件 AMap.event.addListener(marker, 'click', markerClick); } function markerClick(e) { var data = e.target.content; var dataHtml = "<div><h5>" + data.stationName + "</h5>"; dataHtml += "<div>狀態:<b>" + data.typeName + "</b></div>"; dataHtml += "<div>地址:<b>" + data.stationAddress + "</br>" + data.stationAddressDetail + "</b></div>"; dataHtml += "<div>創建時間:<b>" + data.createTime + "</b></div>"; dataHtml += "</div>"; $scope.mapInfoWindow.setContent(dataHtml); $scope.mapInfoWindow.open($scope.map, e.target.getPosition()); } }
二、頁面中
<div class="item t3"> <div class="item-con"> <div class="title">地圖</div> <div class="map-div"> <div id="container"></div> </div> </div> </div> <!-- 高德地圖所需 --> <!-- 放在jquery前解決AMap is not defined 沖突 --> <script type="text/javascript" charset="utf-8" src="http://webapi.amap.com/maps?v=1.3&key=申請的key&plugin=AMap.Autocomplete,AMap.PlaceSearch"></script> <!-- 高德地圖所需 --> <script src="library/jquery-1.12.1.min.js"></script> <script src="library/angularJs_1.2.30/angular.js"></script>
三、效果展示