【无标题】vue+高德地图 搜索+搜索提示+标点定位+详细地址+经纬度


npm i @amap/amap-jsapi-loader –save
<template>
  <div>
    <div>经度:{{lat}}</div>
    <div>纬度:{{lng}}</div>
    <div id="myPageTop">
      <label>请输入关键字:</label>
      <el-input size="mini" v-model="keyword" id="tipinput" style="width: 200px;"/>
      <el-button type="primary" @click="geoCode" size="mini">搜索</el-button>
    </div>
    <div id="container"></div>
  </div>
</template>
<script>
window._AMapSecurityConfig = {
  securityJsCode: "你的密钥"
};
import AMapLoader from "@amap/amap-jsapi-loader";
export default {
  mounted() {
    this.initMap();
  },
  data() {
    return {
      keyword: "广州生物岛",//关键字
      lat: "",//纬度
      lng: "",//经度
      auto: null,//查询
      placeSearch: null,//查询类
      geocoder: null,//转码
      marker: null,//标记点
    };
  },
  methods: {
    initMap() {
      AMapLoader.load({
        key: "你的key", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [
          "AMap.PlaceSearch",
          "AMap.Geocoder",
          "AMap.AutoComplete",
          "AMap.Marker"
        ] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then(AMap => {
        const vm = this;
        this.map = new AMap.Map("container", {
          //设置地图容器id
          // viewMode: "3D", //是否为3D地图模式
          resizeEnable: true,
          zoom: 16, //初始化地图级别
          center: [105.602725, 37.076636] //初始化地图中心点位置
        });
        this.geocoder = new AMap.Geocoder({
          // city: "010" //城市设为北京,默认:“全国”
        });
        this.geoCode();
        this.marker = new AMap.Marker();
        this.map.on("click", this.showInfoClick);
        //----------------------
        let autoOptions = {
          input: "tipinput"
        };
        this.auto = new AMap.AutoComplete(autoOptions);
        this.placeSearch = new AMap.PlaceSearch({
          map: vm.map
        });
        //构造地点查询类
        this.auto.on("select", this.select); //注册监听,当选中某条记录时会触发
      });
    },
    select(e) {
      this.keyword = e.poi.name
      this.placeSearch.setCity(e.poi.adcode);
      this.placeSearch.search(this.keyword); //关键字查询查询
      this.geoCode()
    },
    // 点击事件
    showInfoClick(e) {
      const vm = this;
      this.lat = e.lnglat.getLat();
      this.lng = e.lnglat.getLng();
      var lnglat = [e.lnglat.getLng(), e.lnglat.getLat()];
      this.getNewAddress(lnglat);
    },
    // 加标记
    addMarker(lnglat, address) {
      this.marker.setPosition(lnglat);
      this.map.add(this.marker); // 将创建的点标记添加到已有的地图实例
      this.map.setFitView(this.marker); //移动中心
      //   this.marker.setTitle('我是marker的title');
      this.marker.setLabel({
        direction: "top",
        offset: new AMap.Pixel(0, -10), //设置文本标注偏移量
        content: address //设置文本标注内容
      });
    },
    // 经纬度转中文
    getNewAddress(lnglat) {
      const vm = this;
      vm.geocoder.getAddress(lnglat, function(status, result) {
        console.log(result);
        if (status === "complete" && result.regeocode) {
          var address = result.regeocode.formattedAddress;
          vm.addMarker(lnglat, address);
        } else {
          console.log("根据经纬度查询地址失败");
        }
      });
    },
    // 中文转经纬度
    geoCode() {
      const vm = this;
      if (!vm.keyword) {
        this.$message.warning("请输入关键字");
        return;
      }
      //   var address = document.getElementById("address").value;
      var address = vm.keyword;
      this.geocoder.getLocation(address, function(status, result) {
        if (status === "complete" && result.geocodes.length) {
          console.log(result.geocodes);
          var lnglat = result.geocodes[0].location;
          //   document.getElementById("lnglat").value = lnglat;
          vm.lat = lnglat.lat;
          vm.lng = lnglat.lng;
          var lnglat = [vm.lng, vm.lat];
          //   反解
          vm.getNewAddress(lnglat);
          // var targetAddress = result.geocodes[0].formattedAddress
          // vm.addMarker(lnglat, targetAddress);
        } else {
          console.log("根据地址查询位置失败");
        }
      });
    }
  }
};
</script>
<style scoped>
#container {
  width: 800px;
  height: 300px;
}
</style>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用高德地图 JavaScript API 中的 AMap.Geolocation 对象来确认用户当前的位置。具体步骤如下: 1. 引入高德地图 JavaScript API: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=您的高德地图API Key"></script> ``` 2. 创建地图实例: ```javascript var map = new AMap.Map('map-container', { zoom: 13 }); ``` 3. 创建 AMap.Geolocation 实例: ```javascript var geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为false timeout: 10000, // 超过10秒后停止定位,默认为5s maximumAge: 0, // 定位结果缓存0毫秒,默认值为0 convert: true, // 是否使用坐标转换服务,默认为true showButton: true, // 是否显示定位按钮,默认为false buttonPosition: 'RB', // 定位按钮的位置,默认为'LB',左下角 buttonOffset: new AMap.Pixel(10, 10), // 定位按钮距离地图边缘的偏移量 showMarker: true, // 是否显示定位点,默认为true markerOptions: { icon: new AMap.Icon({ size: new AMap.Size(24, 24), image: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png', imageSize: new AMap.Size(24, 24) }) }, // 定位点的图标样式 showCircle: true, // 是否显示定位精度圆,默认为true circleOptions: { strokeColor: '#0093FF', fillColor: '#0093FF', strokeWeight: 1, fillOpacity: 0.3 } // 定位精度圆的样式 }); ``` 4. 调用 AMap.Geolocation 的 getCurrentPosition 方法进行定位: ```javascript geolocation.getCurrentPosition(function(status, result) { if (status === 'complete') { // 定位成功 var position = result.position; // 获取定位结果的经纬度信息 map.setCenter(position); // 将地图中心移动到定位结果的位置 } else { // 定位失败 console.log('定位失败:' + result.message); } }); ``` 以上代码将会在地图中心显示当前位置的定位点,并在定位精度圆内显示精度范围。 注意:在使用高德地图 JavaScript API 进行地图开发时,需要在高德开放平台申请 API Key 并进行授权。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值