麻烦老师检查一下!

来源:3-12 自由编程

前端菜鸟HH

2020-09-17 11:50:19

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>高德地图</title>

<style>

*{

margin: 0;

padding: 0;

}

input{

outline: none;

}

#container{

width: 100%;

height: 100%;

position: absolute;

top: 0;

left: 0;

}

.box{

width: 350px;

height: 250px;

background: #fff;

position: absolute;

top: 10px;

left: 10px;

box-shadow: 0 0 15px #000;

z-index: 5;

text-align: center;

padding: 10px;

}

.box p{

margin: 10px 0;

}

.box p label{

width: 100px;

text-align: right;

display: inline-block;

margin-right: 10px;

}

.box p input{

height: 24px;

line-height: 24px;

padding: 5px;

width: 120px;

}

#btnCity,

#btnLevel{

background: blue;

color: #fff;

border: none;

height: 38px;

line-height: 38px;

width: 60px;

cursor: pointer;

}

#setRange{

border-radius: 0;

background: #999;

border: 0;

width: 90px;

height: 30px;

line-height: 30px;

cursor: pointer;

}

.currentLocation{

position: absolute;

bottom: 10px;

right: 10px;

}

</style>

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=75776648e2fd2fd601c4de9518b89b65"></script>

</head>

<body>

<div id="container">

<div class="box">

<h2>工具栏</h2>

<p><label>搜索城市</label><input type="text" id="setCity" /> <button id="btnCity">确定</button></p>

<p><label>设置显示级别</label><input type="text" id="setLevel" /> <button id="btnLevel">确定</button></p>

<button id="setRange">解除范围限制</button>

<div class="currentLocation">当前所在省/直辖市:<span id="getLocation"></span></div>

</div>

</div>

<script type="text/javascript">

var map = new AMap.Map('container', {

zoom: 11,

center: [116.397428, 39.90923]

});

var flag = true;

// 默认限制地图显示在北京

var myBounds = map.getBounds([39.997639,116.89112611994148],[39.778484011764014,116.22422]);

// console.log(myBounds);

// 右上角

// myBounds.northeast.Q = 39.997639;

// myBounds.northeast.R = 116.89112611994148;

// // 左下角

// myBounds.southwest.Q = 39.778484011764014;

// myBounds.southwest.R = 116.22422;

map.setLimitBounds(myBounds);

// 解除地图限制

setRange.onclick = function(){

if(flag){

map.clearLimitBounds();

this.innerHTML = '已解除范围限制';

flag = false;

}else{

myBounds = map.getBounds();

map.setLimitBounds(myBounds);

this.innerHTML = '解除范围限制';

flag = true;

}

}

// 搜索城市

btnCity.onclick = function(){

map.setCity(setCity.value);

getLocation.innerHTML = setCity.value;

}

// 设置显示级别

btnLevel.onclick = function(){

map.setZoom(setLevel.value);

}

// 当前所在省/直辖市

map.on('moveend', function(){

map.getCity(function(info){

getLocation.innerHTML = info.province + ',' + info.district;

});

})

// 经纬度设置成中心点

map.on('click', function(e){

// console.log(e);

map.setCenter(e.lnglat);

})

</script>

</body>

</html>


写回答

1回答

好帮手慕慕子

2020-09-17

同学你好,代码实现是正确的,继续加油,祝学习愉快~

0

0 学习 · 6815 问题

查看课程