• 高德地图获取行政区域并且获取经纬度


    我们的需求是获取行政图的切片图,需要四个角的经纬度代码如下

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    7. <title>拉框获取边界经纬度title>
    8. <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
    9. <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    10. <script src="http://webapi.amap.com/maps?v=1.4.15&key=&plugin=AMap.MouseTool,AMap.DistrictSearch">script>
    11. head>
    12. <style>
    13. html,
    14. body,
    15. #container {
    16. margin: 0;
    17. height: 100%;
    18. }
    19. .input-item-text {
    20. width: 7rem;
    21. }
    22. style>
    23. <body>
    24. <div id="container">div>
    25. <div class="input-card">
    26. <label style='color:grey'>行政区边界查询label>
    27. <div class="input-item">
    28. <div class="input-item-prepend">
    29. <span class="input-item-text">行政级别span>
    30. div>
    31. <select id="level">
    32. <option value="district">districtoption>
    33. <option value="city">cityoption>
    34. <option value="province">provinceoption>
    35. select>
    36. div>
    37. <div class="input-item">
    38. <div class="input-item-prepend">
    39. <span class="input-item-text">名称/adcodespan>
    40. div>
    41. <input id='district' type="text" value='泸溪县'>
    42. div>
    43. <input id="draw1" type="button" class="btn" value="查询" />
    44. div>
    45. <script>
    46. var map = new AMap.Map('container', {
    47. center: [110.21682, 28.2205],//地图中心点
    48. zoom: 10 //地图显示的缩放级别
    49. });
    50. var mouseTool = new AMap.MouseTool(map); //在地图中添加MouseTool插件
    51. var drawRectangle = mouseTool.rectangle(); //用鼠标工具画矩形
    52. AMap.event.addListener( mouseTool,'draw',function(e){ //添加事件
    53. console.log(e.obj.getPath());//获取路径
    54. });
    55. var district = null;
    56. var polygon;
    57. function drawBounds() {
    58. //加载行政区划插件
    59. if (!district) {
    60. //实例化DistrictSearch
    61. var opts = {
    62. subdistrict: 0, //获取边界不需要返回下级行政区
    63. extensions: 'all', //返回行政区边界坐标组等具体信息
    64. level: 'district' //查询行政级别为 市
    65. };
    66. district = new AMap.DistrictSearch(opts);
    67. }
    68. console.log(district)
    69. //行政区查询
    70. district.setLevel(document.getElementById('level').value)
    71. district.search(document.getElementById('district').value, function (status, result) {
    72. console.log(status, result)
    73. if (polygon) {
    74. map.remove(polygon)//清除上次结果
    75. polygon = null;
    76. }
    77. var bounds = result.districtList[0].boundaries;
    78. if (bounds) {
    79. //生成行政区划polygon
    80. for (var i = 0; i < bounds.length; i += 1) {//构造MultiPolygon的path
    81. bounds[i] = [bounds[i]]
    82. }
    83. polygon = new AMap.Polygon({
    84. strokeWeight: 1,
    85. path: bounds,
    86. fillOpacity: 0.4,
    87. fillColor: '#80d8ff',
    88. strokeColor: '#0091ea'
    89. });
    90. map.add(polygon)
    91. map.setFitView(polygon);//视口自适应
    92. }
    93. });
    94. }
    95. drawBounds();
    96. document.getElementById('draw1').onclick = drawBounds;
    97. document.getElementById('district').onkeydown = function (e) {
    98. if (e.keyCode === 13) {
    99. drawBounds();
    100. return false;
    101. }
    102. return true;
    103. };
    104. script>
    105. body>
    106. html>

    效果如下

  • 相关阅读:
    【MySql系列】深入解析数据库索引
    docker基础总结
    在vscode中使用Latex:TexLive2023
    Python——缩进和选择
    【iOS】JSONModel的基本使用
    【分享】如何让压缩包里的文件“限制编辑”?
    聚观早报 | 脸书泄露数据被罚20亿;iPhone15将全系支持灵动岛
    C++:类和对象(三)
    java毕业设计鞍山丘比特房屋租赁管理系统Mybatis+系统+数据库+调试部署
    销售技巧之所遵循原则
  • 原文地址:https://blog.csdn.net/baidu_41899377/article/details/133794725