最終效果:
1.接收使用者輸入地點
2.點選查詢
3.中國地圖出現
4.使用者輸入的地點用圓點標記出來
5.並且右上角可以下載成圖片
結構:
一層結構:my-demo資料夾
二層結構:map資料夾、bootstrap.css、china.js、echarts.min.js、jquery.min.js
map.html所有程式碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>map</title>
<link rel="stylesheet" href="../bootstrap.min.css">
<style>
body{
padding-top:80px
}
#search-btn{
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3 col-md-offset-3">
<form class="form-horizontal">
<input id="place-input" type="text" class="form-control" value="西湖" placeholder="請輸入地址">
<button type="button" id="search-btn" class="btn btn-success">
查詢
</button>
</form>
</div>
<div class="col-md-6">
<div id="main" style="height:500px;"></div>
</div>
</div>
</div>
<script src='../jquery.min.js'></script>
<script src='../echarts.min.js'></script>
<script src='../china.js'></script>
<script>
// 百度地圖的開發者祕鑰
var token = 'fHrNQj6DHTjZtfTvfqbsuvTzKc5V9SBl'
var url = 'http://api.map.baidu.com/geocoder/v2/?output=json&ak=' + token + '&address='
var ePlaceInput = $('#place-input')
var eSearchBtn = $('#search-btn')
var myChart = echarts.init(document.getElementById('main'))
var chartData = []
eSearchBtn.click(function() {
var place = ePlaceInput.val()
if (place) {
$.getJSON(url + place + '&callback=?', function(res) {
var loc
if (res.status === 0) {
loc = res.result.location
chartData.push({
name: name,
value: [loc.lng, loc.lat]
})
drawMap(place)
}else{
alert('百度沒有找到地址資訊')
}
})
}
})
function drawMap(name) {
var option = {
backgroundColor: 'pink',
title: {
text: '2019要去的地方',
left: 'center',
textStyle: {
color: '#fff'
}
},
tooltip: {
trigger: 'item'
},
toolbox: {
show: true,
feature: {
saveAsImage: {
show: true
}
}
},
geo: {
map: 'china',
label: {
emphasis: {
show: false
}
},
roam: true,
itemStyle: {
normal: {
areaColor: 'yellow',
borderColor: '#111'
},
emphasis: {
areaColor: 'skyblue'
}
}
},
series: [{
name: '地址',
type: 'scatter',
coordinateSystem: 'geo',
data: chartData,
symbolSize: function(val) {
return 10;
},
}]
}
myChart.setOption(option)
}
</script>
</body>
</html>複製程式碼