腾讯地图JavaScript API GL 版标记及输入经纬度反向定位(vue.js实现)

在这里插入图片描述

<template>
<div>
	<el-form-item label="经度"  prop="longitude" >
        <el-input  v-model="longitude"  @input="creatMapPoint" />
	</el-form-item>
	<el-form-item label="纬度" prop="latitude">
       <el-input  v-model="latitude"  @input="creatMapPoint" />
    </el-form-item>
     <div id="tenXMap" class="tenXMap"/>
</div>
</template>
<script>
 data() {
	 return {
	  longitude: 116.397666,
      latitude: 39.907947
      markerLayer: {},
      map: {},
	 }
 }
mounted() {
  		// 清空标记
    this.markerLayer = new window.TMap.MultiMarker({})
      // 腾讯地图初始化
    this.initMap()
  },
 methods: {
    initMap() {
      const that = this
      const center = new window.TMap.LatLng(Number(this.latitude), Number(this.longitude))
      that.map = new window.TMap.Map(document.getElementById('tenXMap'), {
        center: center,
        zoom: 11
      })
      const latLng = {
        height: 0,
        lat: Number(this.latitude),
        lng: Number(this.longitude)
      }
      this.addImgMarker(latLng)
      that.map.on('click', (e) => {
        that.markerLayer.setGeometries([])
        const position = e.latLng
        that.addImgMarker(position)
        that.latitude = e.latLng.getLat().toFixed(6)
        that.longitude = e.latLng.getLng().toFixed(6)
      })
    }// 地图标记
    addImgMarker(data) {
      this.markerLayer = new window.TMap.MultiMarker({
        map: this.map,
        styles: {
          myStyle: new window.TMap.MarkerStyle({
            width: 25, // 点标记样式宽度(像素)
            height: 30, // 点标记样式高度(像素)
            src: this.imgurl, // 自定义的标记图标
            anchor: { x: 16, y: 32 }
          })
        },
        geometries: [
          {
            id: '0',
            styleId: 'myStyle',
            position: new window.TMap.LatLng(data.lat, data.lng)
          }
        ]
      })
    },
    creatMapPoint() { // 根据输入框坐标定位
      if (this.markerLayer) {
        this.markerLayer.setMap(null)
      }
      const latLng = {
        height: 0,
        lat: Number(this.latitude),
        lng: Number(this.longitude)
      }
      this.markerLayer.setGeometries([])
      this.map.easeTo({ center: new window.TMap.LatLng(Number(this.showForm.latitude), Number(this.showForm.longitude)), zoom: 11, rotation: 0 }, { duration: 1000 }) // 平滑缩放,旋转到指定级别
      this.addImgMarker(latLng)
    },
 }
 </script>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用腾讯地图JavaScript API来获取当前位置的经纬度。以下是一个使用Vue.js编写的示例代码: 1. 在`index.html`中引入腾讯地图JavaScript API: ```html <script src="https://map.qq.com/api/js?v=2.exp&key=YOUR_KEY"></script> ``` 其中,`YOUR_KEY`需要替换成你自己的腾讯地图API密钥。 2. 在Vue组件中添加一个地图容器: ```html <template> <div ref="mapContainer" style="width: 100%; height: 400px;"></div> </template> ``` 3. 在Vue组件的`mounted`生命周期钩子中初始化地图,并获取当前位置的经纬度: ```js mounted() { // 初始化地图 this.initMap(); }, methods: { initMap() { // 创建地图实例 const map = new qq.maps.Map(this.$refs.mapContainer, { center: new qq.maps.LatLng(39.916527, 116.397128), // 默认北京市中心 zoom: 13, }); // 获取当前位置的经纬度 const geolocation = new qq.maps.Geolocation(); geolocation.getLocation( (position) => { const latLng = new qq.maps.LatLng( position.lat, position.lng ); // 将地图中心设置为当前位置 map.setCenter(latLng); }, () => { console.error("定位失败"); } ); }, } ``` 在上面的代码中,我们首先创建了一个地图实例,并将地图容器绑定到Vue组件的`$refs`属性中。然后,我们使用腾讯地图的Geolocation类来获取当前位置的经纬度,并将地图中心设置为当前位置。如果定位失败,则会在控制台输出错误信息。 注意:在使用腾讯地图API时,需要先在腾讯云上开通地图API服务,并申请密钥。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值