• webGIS轨迹移动案例(高德)


    学习webgis可以参考我的专栏,代码学习来自B站的某一位视频博主

    (注:在引入资源的部分,都需要你自己的key和安全密匙)

    最后的webgis案例

    效果图:

     首先创建如下文件夹结构

     index.html

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>智慧校园title>
    8. <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    9. <link rel="stylesheet" href="./css/index.css">
    10. <script type="text/javascript">
    11. window._AMapSecurityConfig = {
    12. securityJsCode:'你的密匙',
    13. }
    14. script>
    15. <script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=你的key&plugin=AMap.GeoJSON">script>
    16. <script src="./js/store.js">script>
    17. head>
    18. <body>
    19. <div id="container">div>
    20. <div class="info">点击地图标注热门地点div>
    21. <div class="input-card">
    22. <h4>推荐路线h4>
    23. <div class="input-item">
    24. <button class="btn" onclick="startAnimation()">开始动画button>
    25. div>
    26. div>
    27. <script>
    28. // 定义一个变量,保存一个对象
    29. var map = new AMap.Map('container',{
    30. center:[114,30],//设置地图中心点的经纬度
    31. zoom:12, //地图的缩放比例(3-20)
    32. viewMode:'3D', //放大后有3D效果
    33. pitch:45 //3D俯视角度
    34. })
    35. // 加载控件
    36. // 5.1安装plugin
    37. AMap.plugin([
    38. 'AMap.ToolBar',
    39. 'AMap.Scale',
    40. 'AMap.HawkEye',
    41. 'AMap.MapType',
    42. 'AMap.Geolocation',
    43. 'AMap.ControlBar',
    44. 'AMap.MoveAnimation'
    45. ],
    46. function(){
    47. map.addControl(new AMap.ToolBar({
    48. position:{
    49. top:'180px',
    50. right:'40px'
    51. }
    52. })),
    53. map.addControl(new AMap.HawkEye({isOpen:true}));
    54. map.addControl(new AMap.MapType({
    55. position:{
    56. top:'300px',
    57. right:'40px'
    58. }
    59. }));
    60. map.addControl(new AMap.Geolocation());
    61. map.addControl(new AMap.ControlBar());
    62. // map.addControl(new AMap.MoveAnimation());
    63. });
    64. //定义一个全局变量
    65. var geojson = new AMap.GeoJSON({
    66. geoJSON:null,
    67. })
    68. // 如果存在数据,那么才导入数据
    69. if(JSON.stringify(getData())!='[]'){
    70. // 导入数据
    71. geojson.importData(getData());
    72. // 恢复旧数据的点击事件
    73. geojson.eachOverlay(function(item){
    74. item.on('click',function(e){
    75. // 让点击的marker对象+1
    76. var ext = item.getExtData()
    77. var click = ++ext._geoJsonProperties.click
    78. var infowindow = new AMap.InfoWindow({
    79. anchor:'top-center',
    80. content:`
      打卡了${click}
      `
    81. })
    82. // 显示(打开信息窗口)
    83. infowindow.open(map,item.getPosition())
    84. saveData(geojson.toGeoJSON())
    85. })
    86. })
    87. }
    88. map.add(geojson)
    89. // 监听地图的点击事件
    90. map.on('click',function(e){
    91. var marker = new AMap.Marker({
    92. position:e.lnglat,
    93. extData:{
    94. _geoJsonProperties:{
    95. gid:geojson.getOverlays().length+1,
    96. click:0
    97. }
    98. }
    99. })
    100. // 使用覆盖物的点击事件
    101. marker.on('click',function(e){
    102. // 让点击的marker对象+1
    103. var ext = marker.getExtData()
    104. var click = ++ext._geoJsonProperties.click
    105. // 消息提示
    106. var infowindow = new AMap.InfoWindow({
    107. anchor:'top-center',
    108. content:`
      打卡了${click}
      `
    109. })
    110. // 显示(打开信息窗口)
    111. infowindow.open(map,marker.getPosition())
    112. saveData(geojson.toGeoJSON())
    113. })
    114. // 通过geojson来管理覆盖物
    115. geojson.addOverlay(marker)
    116. // 将geojson这个对象转换成标准的geojson格式
    117. saveData(geojson.toGeoJSON())
    118. })
    119. // 点击路径规划事件
    120. function startAnimation(){
    121. // 实现路径规划(导航)
    122. AMap.plugin('AMap.Driving',function(){
    123. var driving = new AMap.Driving({
    124. map:map,
    125. // 驾车策略
    126. policy:AMap.DrivingPolicy.LEAST_TIME,
    127. })
    128. // 设置起点和终点
    129. var start = new AMap.LngLat(114.4000,30.5812)
    130. var end = new AMap.LngLat(114.342,30.24221)
    131. // 通过geojson得到每一个点的坐标
    132. var opts = {
    133. waypoints:[]
    134. }
    135. //将每个点都传进数组里面去
    136. geojson.eachOverlay(function(item){
    137. opts.waypoints.push(item.getPosition())
    138. })
    139. driving.search(start,end,opts,function(status,result){
    140. if(status=='complete'){
    141. var lineArr =[]
    142. // 把每个点添加进数组
    143. result.routes[0].steps.forEach(function(item){
    144. lineArr.push(...item.path)
    145. })
    146. // 轨迹模拟
    147. var marker = new AMap.Marker({
    148. map:map,
    149. position:start,
    150. icon:'https://webapi.amap.com/images/car.png',
    151. offset:new AMap.Pixel(-26,-13),
    152. autoRotation:true,
    153. angle:-180
    154. })
    155. var passedPolyline = new AMap.Polyline({
    156. map:map,
    157. strokeColor:"#AF5",
    158. strokeWeight:6
    159. })
    160. marker.on('moving',function(e){
    161. passedPolyline.setPath(e.passedPath)
    162. })
    163. // 展示
    164. map.setFitView()
    165. // 每隔一段时间移动
    166. marker.moveAlong(lineArr,{
    167. duration:500,
    168. // autoRotation:true
    169. })
    170. }else{
    171. console.log("规划失败");
    172. }
    173. })
    174. })
    175. }
    176. script>
    177. html>

    js文件夹中,创建一个store.js文件

    1. // 从localstorage读取数据
    2. function getData(){
    3. // 如果本地的localstorage中不存在数据
    4. if(!localStorage.getItem('geojson')){
    5. localStorage.setItem('geojson','[]')
    6. }
    7. return JSON.parse(localStorage.getItem('geojson'))
    8. }
    9. // 将数据保存到loclastorage中
    10. function saveData(data){
    11. localStorage.setItem('geojson',JSON.stringify(data))
    12. }

    css文件夹中,创建一个index.css

    1. html,body
    2. #container{
    3. width: 1200px;
    4. height: 600px;
    5. }

  • 相关阅读:
    spring事务
    参照DefenseGrid在Unity中实现合理的塔防寻路机制
    深入理解TDD(测试驱动开发):提升代码质量的利器!
    【C++入门 四】学习C++内联函数 | auto关键字 | 基于范围的for循环(C++11) | 指针空值nullptr(C++11)
    KekeBlog项目实战后台模块(二)(已完结)
    AAPT: error: resource android:attr/lStar not found
    java-php-python-ssm学校旧书交易网站计算机毕业设计
    Salesforce OmniStudio顾问备考攻略
    2022第8届中国大学生程序设计竞赛CCPC桂林站, 签到题4题
    leetCode 62.不同路径 动态规划 + 空间复杂度优化
  • 原文地址:https://blog.csdn.net/qq_50951790/article/details/128044971