進入高德地圖開放平臺,註冊高德地圖帳號、登錄、認證爲開發者,而後進入我的控制檯、建立新應用,在建立的應用上點擊「添加key」按鈕。 git
這裏咱們須要用到的高德地圖的JavaScript API ,因此點擊添加按鈕後咱們選擇Web 服務的JS API如圖 github
// 定義一個變量來引入衛星底圖API
var satellite = new AMap.TileLayer.Satellite({
map: map,
zIndex: 2,
zooms: [3, 18]
})
// 點擊按鈕實現切換衛星底圖,再點擊就關閉衛星底圖。
document.getElementById('btn5').onclick = function() {
if (this.innerText === '衛星') {
this.style.backgroundColor = "skyblue"
this.innerText = '地圖'
satellite.show()
} else if (this.innerText === '地圖') {
this.style.backgroundColor = "white"
this.innerText = '衛星'
satellite.hide()
}
複製代碼
運行結果 web
設計方法算法
var city // 定義一個變量來接收當前地圖中心點所在城市
map.getCity(function (data) {
city = data.city
})
AMap.plugin('AMap.Weather', function() {
var weather = new AMap.Weather()
//根據上面獲取到的當前城市名稱city來搜索當前天氣
weather.getLive(city, function(err, data) {
if (!err) {
var str = []
str.push('<h4 >實時天氣' + '</h4><hr>')
str.push('<p>城市/區:' + data.city + '</p>')
str.push('<p>天氣:' + data.weather + '</p>')
str.push('<p>溫度:' + data.temperature + '℃</p>')
str.push('<p>風向:' + data.windDirection + '</p>')
str.push('<p>風力:' + data.windPower + ' 級</p>')
str.push('<p>空氣溼度:' + data.humidity + '</p>')
str.push('<p>發佈時間:' + data.reportTime + '</p>')
marker = new AMap.Marker({
map: map,
position: map.getCenter()
})
var infoWin = new AMap.InfoWindow({
content: '<div id="weatherShow2" class="info" style="position:inherit;margin-bottom:0;">' + str.join('') + '</div><div class="sharp"></div>',
isCustom: true,
offset: new AMap.Pixel(0, -37)
})
infoWin.open(map, marker.getPosition())
marker.on('mouseover', function() {
infoWin.open(map, marker.getPosition())
})
}
})
複製代碼
var ruler // 定義一個變量來接收距離測量插件
map.plugin(["AMap.RangingTool"], function() {
ruler = new AMap.RangingTool(map)
})
document.getElementById('btn3').onclick = function() {
console.dir(this)
if (this.innerText === '距離測量') {
this.style.backgroundColor = "skyblue"
this.innerText = '關閉測量'
ruler.turnOn()
} else if (this.innerText === '關閉測量') {
this.style.backgroundColor = "white"
this.innerText = '距離測量'
ruler.turnOff()
}
}
複製代碼
// 定義變量來接收指定搜索框用戶輸入的地點,下面的變量tipinput是指頁面元素輸入框的id值,以此來接收用戶在特色輸入框輸入的內容。
var autoOptions = {
input: "tipinput"
}
var auto = new AMap.Autocomplete(autoOptions)
// ===========================================================數據展現
var placeSearch = new AMap.PlaceSearch({
pageSize: 5, // 單頁顯示結果總條數
pageIndex: 1, // 頁碼
panel: "panel", // 結果列表將在此容器中進行展現
city: "010", // 興趣點城市
citylimit: true, //是否強制限制在設置的城市內搜索
map: map, // 展示結果的當前地圖對象
autoFitView: true // 選擇是否自動調整當前地圖視野來使繪製的 Marker點都處於當前視口的可見範圍內
}) // 構造地點查詢類
AMap.event.addListener(auto, "select", select) //註冊監聽,當選中匹配地點時會觸發
function select(e) {
placeSearch.setCity(e.poi.adcode); //設置當前城市編碼查詢
placeSearch.search(e.poi.name); //關鍵字查詢查詢
}
複製代碼
運行結果數據庫
輸入提示 api
POI搜索bash
var linesearch
var busLineName
/*公交線路查詢*/
function lineSearch() {
busLineName = document.getElementById('BusLineName').value
document.getElementById('input-card').style.display = "block"
console.log(busLineName)
if (!busLineName) return
//定義變量先實例化公交線路查詢,而後選擇取回一條路線
if (!linesearch) {
linesearch = new AMap.LineSearch({
pageIndex: 1,
city: '梅州',
pageSize: 1,
extensions: 'all'
})
}
//搜索「5」相關公交線路
linesearch.search(busLineName, function (status, result) {
map.clearMap()
if (status === 'complete' && result.info === 'OK') {
lineSearch_Callback(result)
} else {
alert(result)
}
})
}
function lineSearch_Callback(data) {
var lineArr = data.lineInfo
var lineNum = data.lineInfo.length
if (lineNum == 0) {} else {
for (var i = 0; i < lineNum; i++) {
var pathArr = lineArr[i].path;
var stops = lineArr[i].via_stops;
var startPot = stops[0].location;
var endPot = stops[stops.length - 1].location;
if (i == 0) //做爲示例,只繪製一條線路
drawbusLine(startPot, endPot, pathArr);
}
}
}
/*繪製路線*/
function drawbusLine(startPot, endPot, BusArr) {
//繪製起點,終點
new AMap.Marker({
map: map,
position: startPot, //基點位置
icon: "https://webapi.amap.com/theme/v1.3/markers/n/start.png",
zIndex: 10
})
new AMap.Marker({
map: map,
position: endPot, //基點位置
icon: "https://webapi.amap.com/theme/v1.3/markers/n/end.png",
zIndex: 10
})
//繪製乘車的路線
busPolyline = new AMap.Polyline({
map: map,
path: BusArr,
strokeColor: "#09f", //線顏色
strokeOpacity: 0.8, //線透明度
isOutline: true,
outlineColor: '#C4C4C4',
strokeWeight: 6 //線寬
})
map.setFitView()
}
document.getElementById('search').onclick = lineSearch
document.getElementById('btn7').onclick = function () {
if (this.innerText === '公交查詢') {
this.style.backgroundColor = "skyblue"
this.innerText = '關閉查詢'
lineSearch()
} else if (this.innerText === '關閉查詢') {
this.style.backgroundColor = "#C4C4C4"
this.innerText = '公交查詢'
document.getElementById('input-card').style.display = "none"
map.clearMap()
}
}
複製代碼
// =====================================================自定義右鍵菜單類
var menu = new ContextMenu(map)
function ContextMenu(map) {
var me = this
// 地圖中添加鼠標工具MouseTool插件
this.mouseTool = new AMap.MouseTool(map)
this.contextMenuPositon = null
var content = [] // 建立一個容器來設置右鍵菜單的樣式
content.push("<div class='info context_menu'>")
content.push(" <p onclick='menu.zoomMenu(0)'>縮小</p>")
content.push(" <p class='split_line' onclick='menu.zoomMenu(1)'>放大</p>")
content.push(" <i class='menu-icon menu-icon-from'></i><p onclick='menu.addOrigin()'>設爲起點</p>")
content.push(" <p onclick='menu.addWaypoints()'>設爲途經點</p>")
content.push(" <p onclick='menu.addDestination()'>設爲終點</p>")
content.push(" <p onclick='menu.addMarkerMenu()'>添加標記獲取經緯度</p>")
content.push("</div>")
// 經過content自定義右鍵菜單內容
this.contextMenu = new AMap.ContextMenu({
isCustom: true,
content: content.join('')
})
//地圖map綁定鼠標右擊事件——彈出右鍵菜單
map.on('rightclick', function(e) {
me.contextMenu.open(map, e.lnglat)
me.contextMenuPositon = e.lnglat //右鍵菜單出現的位置
})
}
ContextMenu.prototype.zoomMenu = function zoomMenu(tag) { // 右鍵菜單縮放地圖
if (tag === 0) {
map.zoomOut()
}
if (tag === 1) {
map.zoomIn()
}
this.contextMenu.close()
}
複製代碼
//=========================封裝獲取當前經緯度方法
function getLngLat() {
var lnglatInput = document.getElementById('lnglat')
lnglatInput.setAttribute('value', lnglat.toString())
}
ContextMenu.prototype.addMarkerMenu = function() { // 右鍵菜單添加Marker標記
this.mouseTool.close()
this.marker = new AMap.Marker({
map: map,
position: this.contextMenuPositon //基點位置
})
this.contextMenu.close()
// ==================infowidnow 窗口的 innerHTML
var infoWindowContent =
'<div id="infoWindow" className="custom-infowindow input-card">' +
'<label style="color:grey">當前地點</label>' +
'<div class="input-item">' +
'<div class="input-item-prepend">' +
'<span class="input-item-text" >經緯度</span>' +
'</div>' +
'<input id="lnglat" type="text" />' +
'</div>' +
// 爲 infowindow 添加自定義事件
'<input id="lnglat2container" type="button" class="btn" value="獲取該位置經緯度" onclick="getLngLat()"/>' +
'</div>'
// 建立自定義內容的 infowindow 實例
this.infoWindow = new AMap.InfoWindow({
position: this.contextMenuPositon,
offset: new AMap.Pixel(0, -35),
content: infoWindowContent
})
this.infoWindow.open(map)
// 將當前經緯度展現在 infowindow 的 input 中
}
複製代碼
// 定義變量引用導航API
var driving = new AMap.Driving({
map: map,
panel: "panel2"
})
// 爲設爲起點封裝方法
ContextMenu.prototype.addOrigin = function() { //右鍵菜單添加Marker標記
this.mouseTool.close()
// 建立一個 Icon
var startIcon = new AMap.Icon({
// 圖標尺寸
size: new AMap.Size(25, 34),
// 圖標的取圖地址
image: 'https://a.amap.com/jsapi_demos/static/demo-center/icons/dir-marker.png',
// 圖標所用圖片大小
imageSize: new AMap.Size(135, 40),
// 圖標取圖偏移量
imageOffset: new AMap.Pixel(-9, -3)
})
this.origin = new AMap.Marker({
map: map,
position: this.contextMenuPositon, //基點位置
icon: startIcon,
offset: new AMap.Pixel(-9, -3)
})
this.contextMenu.close()
this.origin1 = this.contextMenuPositon
}
// 爲設爲途徑點封裝方法
ContextMenu.prototype.addWaypoints = function() { //右鍵菜單添加Marker標記
this.mouseTool.close()
var viaIcon = new AMap.Icon({
size: new AMap.Size(25, 34),
// 圖標的取圖地址
image: 'https://a.amap.com/jsapi_demos/static/demo-center/icons/dir-via-marker.png',
})
this.waypoints = new AMap.Marker({
map: map,
position: this.contextMenuPositon, //基點位置
icon: viaIcon,
offset: new AMap.Pixel(-9, -3)
})
this.contextMenu.close()
this.waypoints1 = this.contextMenuPositon
}
// 爲設爲終點封裝方法
ContextMenu.prototype.addDestination = function() { //右鍵菜單添加Marker標記
this.mouseTool.close()
var endIcon = new AMap.Icon({
size: new AMap.Size(25, 34),
image: 'https://a.amap.com/jsapi_demos/static/demo-center/icons/dir-marker.png',
imageSize: new AMap.Size(135, 40),
imageOffset: new AMap.Pixel(-95, -3)
})
this.destination = new AMap.Marker({
map: map,
position: this.contextMenuPositon, //基點位置
icon: endIcon,
offset: new AMap.Pixel(-9, -3)
})
this.contextMenu.close()
this.destination1 = this.contextMenuPositon
// 根據起終點經緯度規劃駕車導航路線
driving.search(this.origin1, this.destination1, {
waypoints: [this.waypoints1]
}, function(status, result) {
// result 便是對應的駕車導航信息
if (status === 'complete') {
log.success('繪製駕車路線完成')
// 由於駕車導航會自動生成相應的點標記,因此以前經過右鍵設置的點標記要隱藏
menu.origin.hide()
menu.waypoints.hide()
menu.destination.hide()
} else {
log.error('獲取駕車數據失敗:' + result)
}
})
}
複製代碼
源碼連接ide