HTML5获取手机方位角并在地图实时显示

1 技术路线

HTML5、Vue、Leaflet(用于呈现地图)、leaflet-rotatedmarker(用于旋转marker定位点),需要https部署。

2 实现流程

2.1 初始化leaflet并显示地图:

initMap() {
   
   
      var tVue = this;
      tVue.map = L.map("map", {
   
   
        center: [36.66374, 117.141833],
        zoom: 15,
        crs: L.CRS.EPSG4326
      });

      var token = "你的token";
      var tdt_sd = L.tileLayer('https://service.sdmap.gov.cn/tileservice/sdpubmap?layer=SDPubMap&style=default&tilematrixset=default028mm&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image%2Fpng&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + token, {
   
   
        zoomOffset: 1, // 注意crs为EPSG4326有偏移
        noWrap: true,
        bounds: [[-90, -180], [90, 180]],
        crs: L.CRS.EPSG4326
      });
      tVue.map.addLayer(tdt_sd);
    }

2.2 获取当前位置,添加定位图标

var tVue = this;
//获取当前位置
navigator.geolocation.getCurrentPosition(function (position) {
   
   
   //添加位置图标
   tVue.addLocMarker(position.coords.latitude, position.coords.longitude);
 }, function (err) {
   
   
   console.log("geolocation error", err.message);
 });
addLocMarker(lat, lng) {
   
   
  var tVue = this;
  if (tVue.locMarker == null) {
   
   
    var icon = L.icon({
   
   
      iconUrl: imageCurrPosition,
      iconSize
在Vue中实现这样一个输入和显示起点、终点以及计算大地线长度的组件,可以分为以下几个步骤: 1. 创建Vue组件:首先创建一个新的Vue组件,例如`GeographicInput.vue`,导入必要的依赖如`axios`用于发送API请求(如果需要的话),以及`v-model`指令用于数据绑定。 ```html <template> <div> <input v-model="startLat" placeholder="起点纬度"> <input v-model="startLon" placeholder="起点经度"> <input v-model="direction" placeholder="大地方位角"> <button @click="calculateDistance">计算</button> <h2>终点信息:</h2> <p>纬度: {{endLat}}</p> <p>经度: {{endLon}}</p> <p>大地线长度: {{distance}}</p> </div> </template> <script> import axios from 'axios'; export default { data() { return { startLat: '', startLon: '', direction: '', endLat: '', endLon: '', distance: '' }; }, methods: { async calculateDistance() { // 计算逻辑(这里仅为简化的例子,实际可能需要更复杂的地理编码或网络请求) const result = await this.calculateGreatCircle(startLat, startLon, direction); this.endLat = result.lat; this.endLon = result.lon; this.distance = result.distance; }, calculateGreatCircle(startLat, startLon, direction) { // 可能需要的外部函数,这里仅做占位 console.log(`计算中...`); // 根据提供的起点、方向返回终点经纬度及距离 // 参考之前给出的JavaScript函数 } } }; </script> ``` 2. 实现计算逻辑:你需要在`calculateGreatCircle`方法内部实现计算功能。如果你有一个本地的计算函数,可以直接调用;如果需要通过网络请求(比如Google Maps API或其他服务)计算,记得添加适当的错误处理和请求。 3. 整合API(可选):如果需要用到外部服务来计算大地线长度,你需要设置一个HTTP请求,将起点经纬度、方位角作为参数传递,接收返回的结果更新UI。 请注意,这里的示例假设已经有了起点、方向到终点的直接计算方法。在实际场景中,你可能需要使用地图API或者其他服务来获取准确的地线距离。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值