老师,我的代码是哪里有问题,设置级别没有用,谢谢
来源:3-12 自由编程
qq_慕移动3101913
2020-02-07 16:01:08
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=48259e2fd01bb69855450c5b97b5ea81"></script>
<style type="text/css">
*{margin: 0;padding: 0;}
#container {width:100%; height: 100%; position: absolute;left: 0;top: 0;}
#toolbar{
width:300px;
height: 250px;
background-color: #fff;
position: absolute;
z-index: 99;
left: 10px;
top: 10px;
box-shadow: 1px 1px 5px black;
background: white;
}
.title{
width: 300px;
height: 30px;
text-align: center;
line-height: 30px;
font-weight: bold;
margin-top: 20px;
font-size: 16px;
}
.city{
font-size: 13px;
position: absolute;
right: 37px;
top: 53px;
}
.rank{
font-size: 13px;
position: absolute;
right: 37px;
top: 92px;
}
#btn1{
width: 45px;
height: 30px;
color: #fff;
font-size: 13px;
background-color: #4169E1;
border:none;
}
#btn2{
width: 45px;
height: 30px;
color: #fff;
font-size: 13px;
background-color:#4169E1;
border:none;
}
#clear{
width: 100px;
height: 30px;
font-size: 13px;
background-color: #C0C0C0;
position: absolute;
left: 110px;
top: 131px;
border:none;
}
.kuang{
width: 100px;
height: 25px;
padding-left: 5px;
}
.newest{
position: absolute;
right: 10px;
bottom: 10px;
font-size: 13px;
}
</style>
</head>
<body>
<!-- 地图 -->
<div id="container"></div>
<!-- 工具栏 -->
<div id='toolbar'>
<p class="title">工具栏</p>
<div class="city">搜索城市:
<input class="kuang" type="text" name="" id='city2'>
<button id='btn1'>确定</button>
</div>
<div class="rank">设置显示级别:
<input class="kuang" type="text" name="" id='zoomVal'>
<button id='btn2'>确定</button>
</div>
<button id='clear'>解除范围限制</button>
<p class="newest">当前所在省/直辖市:<span id='ne'></span></p>
</div>
<script type="text/javascript">
//创建一个地图
var map = new AMap.Map('container',{
zoom:10, //初始的地图级别
center:[116.379391,39.861536] //初始化地图的中心点
});
//设置地图显示范围为北京
var myBounds = new AMap.Bounds([116.22422,39.813285],[116.567542,39.997639]);
//限制地图显示在北京
map.setBounds(myBounds);
map.setLimitBounds(myBounds);
//点击按钮搜索城市
btn1.onclick = function(){
map.setCity(city2.value);
};
//点击按钮搜索地图级别
btn2.onclick = function(){
map.setZoom(zoomVal.value);
};
//通过事件来给予中心点
map.on('click',function(e){
map.setCenter([e.lnglat.lng,e.lnglat.lat])
});
//通过事件解除显示限制
clear.onclick = function(){
if(clear.innerHTML === '解除范围限制'){
map.clearLimitBounds(myBounds);
clear.innerHTML = '已解除范围限制'
}else if(clear.innerHTML === '已解除范围限制'){
map.setLimitBounds(myBounds);
clear.innerHTML = '解除范围限制'
}
};
//为鼠标绑定moveend事件 ,当鼠标在地图上移动时 ,让工具栏的右下角显示当前省/直辖市。
map.on('moveend',function(){
map.getCity(function(info){
ne.innerHTML=info.province;
})
});
</script>
</body>
</html>预算效果是:

可现在是:(我修改:初始的地图级别没效果!
var map = new AMap.Map('container',{
zoom:10, //初始的地图级别
center:[116.379391,39.861536] //初始化地图的中心点
});
)

1回答
好帮手慕星星
2020-02-07
同学你好,修改初始的地图级别没效果是因为限制了显示范围,注释掉就可以了

效果

代码还不完善:
1、需要在搜索城市的时候解除范围限制,地图才能正常显示

2、限制显示范围的按钮,点击应该限制当前显示市级范围,而不是只是北京哦,还需要修改名字。参考

如果我的回答帮到了你,欢迎采纳,祝学习愉快~
相似问题