获取当前地图的行政区 getcity()
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:300px;height:100px;background-color: #fff;border:1px高中语文答题技巧全套 solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> </div> <script> var map=new amap.map("container"); map.on("moveend",function(){ //获取行政区 map.getcity(function(info){ console.log(info); tzoomnode.innerhtml=info.province+","+info.city+","+info.district; }) }) </script> </body></html>
注意:默认只能获取中国的行政区
设置行政区 tcity()
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:300px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> <input type="text" id="city"> <button id="btn">设置行政区</button> </div> <script> var map=new amap.map("container"); map.on("moveend",function(){ //获取行政区 map.getcity(function(info){ console.log(info); tzoomnode.innerhtml=info.province+","+info.city+","+info.district; }) }) //设置行政区 map.tcity("宁波"); //通过事件设置行政区 btn.onclick=function(){ map.tcity(city.value); } </script> </body></html>
获取地图的范围 getbounds()
c对象
northeast 右上
southwest 左下
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20p妞妞周国平x;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> <span id="ne"></span><br> <span id="sw"></span><br> </div> <script> var map=new amap.map("container"); map.on("moveend",function(){ //获取范围 console.log(map.getbounds()); ne.innerhtml=map.getbounds().northeast.tostring(); sw.innerhtml=map.getbounds().southwest.tostring(); }) </script> </body></html>
设置地图的显示范围 tbounds()
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> <span id="ne"></span><br> <span id="sw"></span><br> </div> <script> var map=new amap.map("container"); //先左下角,再右上角 var mybounds=new amap.bounds([122.240801,29.401671],[123.539934,30.261788]); map.tbounds(mybounds); //设置的范围并不一定完全贴合获取到的范围,只能是尽可能匹配 console.log(map.getbounds().northeast.tostring()); console.log(map.getbounds().southwest.tostring()); </script> </body></html>
限制显示范围 tlimitbounds()
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> <span id="ne"></span><br> <span id="sw"></span><br> </div> <script> var map=new amap.map("container"); //限制显示范围 var bounds=map.getbounds(); map.tlimitbounds(bounds); </script> </body></html>
限制在当前范围内不可查看其它范围的
解除范围限制 clearlimitbounds()
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> <span id="ne"></span><br> <span id="sw"></span><br> </div> <script> var map=new amap.map("container"); //限制显示范围 var bounds=map.getbounds(); map.tlimitbounds(bounds); //解除范围限制 map.clearlimitbounds(); </script> </body></html>
控制单个坐标值的限制
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> <span id="ne"></span><br> <span id="sw"></span><br> </div> <script> var map=new amap.map("container"); //限制显示范围 var bounds=map.getbounds(); console.log(bounds); //单独限制右上角的水平坐标不能超过123 bounds.northeast.r=123; map.tlimitbounds(bounds); //解除范围限制 map.clearlimitbounds(); </script> </body></html>
以上实现右上角的水平范围不会超过123
一个关于显示和解除地图范围的demo
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> 右边水平限制:<input type="text" id="x2"><br> 左边水平限制<input type="text" id="x1"><br> <button id="btn">确定</button> <button id="clear">解除限制</button> </div> <script> var map=new amap.map("container"); //限制显示范围 btn.onclick=function(){ var bounds=map.getbounds(); //input输入的文本是string,需要转为number bounds.northeast.r=number(x2.value); bounds.southwest.r=number(x1.value); map.tlimitbounds(bounds); } //解除显示范围 clear.onclick=function(){ map.clearlimitbounds(); } map.on("moveend",function(){ console.log(map.getbounds().northeast.r); console.log(map.getbounds().southwest.r); }) </script> </body></html>
地图的平移,以像素为单位
panby(左右像素,上下像素) 左正右负,上正下负
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <ti英语四级怎么缴费tle>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> 右边水平限制:<input type="text" id="x2"><br> 左边水平限制<input type="text" id="x1"><br> <button id="btn">确定</button> <button id="clear">解除限制</button> </div> <script> var map=new amap.map("container"); ttimeout(function(){ //向左平移50像素,向上平移30像素 map.panby(50,30); },2000); </script> </body></html>
随机移动
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> </div> <script> var map=new amap.map("container"); //每3秒随机移动 tinterval(function(){ //向左上方向随机移动 //map.panby(30*math.random(),30*math.random()); //不确定方向的随机移动 map.panby(30*math.random()-15,30*math.random()-15); },3000); </script> </body></html>
panto() 移动到指定位置
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> </div> <script> var map=new amap.map("container"); console.log(map.getcenter().tostring()); ttimeout(function(){ // 宁波移动到了绍兴 map.panto([120.549792,29.868388]); },3000); </script> </body></html>
通过事件设置平移
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> <input type="text" id="x"> <input type="text" id="y"> <button id="btn">移动</button> </div> <script> var map=new amap.map("container"); console.log(map.getcenter().tostring()); //通过事件设置 btn.onclick=function(){ map.panto([x.value,y.value]); } </script> </body></html>
获取鼠标的经纬度
longitude 经度
latitude 纬度
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style>幸福就在不远处;</head><body> <div id="container"></div> <div id="tzoomnode"> 鼠标点击的经纬度:<span id="xy"></span> </div> <script> var map=new amap.map("container"); //通过事件设置 map.on("click",function(e){ console.log(e.lnglat); xy.innerhtml=e.lnglat.lng+","+e.lnglat.lat; //同时将鼠标点击位置设置为中心点 map.tcenter([e.lnglat.lng,e.lnglat.lat]); }) </script> </body></html>
以上代码实现鼠标点击的位置作为地图的中心点
设置鼠标样式 tdefaultcursor()
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>map</title> <script type="text/javascript" src="/d/file/titlepic/maps <style> *{margin:0;padding:0;} #container {width:100%; height: 100%;top:0;left:0;position: absolute; } #tzoomnode{width:400px;height:100px;background-color: #fff;border:1px solid;box-shadow:0 0 5px #000;top:20px;right:20px;position: absolute;} </style></head><body> <div id="container"></div> <div id="tzoomnode"> </div> <script> var map=new amap.map("container"); //只要是css的cursor中能设置的属性值,都可以 map.tdefaultcursor("pointer"); </script> </body></html>
本文发布于:2023-04-07 08:30:54,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/002d04a2c323315936e74534c5f7a436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:高德地图API 之行政区+范围+平移+经纬度+鼠标样式.doc
本文 PDF 下载地址:高德地图API 之行政区+范围+平移+经纬度+鼠标样式.pdf
留言与评论(共有 0 条评论) |