Vue uses Amap to realize city positioning

Vue uses Amap to realize city positioning

This article shares the specific code of Vue using Amap to realize city positioning for your reference. The specific content is as follows

Click on the front-end selection box to jump to the map site selection page

<van-row class="address_item">
  <van-col span="6" class="item-title">Delivery address</van-col>
  <van-col span="18">
  <p class="item">
  <van-icon name="arrow" class="arrow"/>
  <van-icon name="location-o" />
  <span><van-field v-model="mapname" placeholder="Click to select" readonly v-on:click="mapaddress"/></span>
  </p>
  </van-col>
</van-row>

Second, trigger the click event

mapaddress(){
   et a=this.$route.query.id
  localStorage.setItem('names',this.names);
  localStorage.setItem('phone',this.phone);
  localStorage.setItem('addressDetail',this.addressDetail);
  localStorage.setItem('postalCode',this.postalCode);
  localStorage.setItem('checked',this.checked);
  this.$router.push({
    path: '/mapLocation',
    query: {id:a,are:this.are}
  })
}

Three map site selection pages

Use ifarm to embed the Amap component. Amap Note that you need to apply for a key to use the map. When applying, please choose the service according to your needs.

<template>
  <iframe id="getAddress" @load="loadiframe"
    src="https://m.amap.com/picker/?keywords=Office building, community, school&zoom=15&center=&radius=1000&total=20&key=the key you applied for"
    style="width:100%; height:100%;position: absolute;z-index:22222;">
  </iframe>
</template>

4. After the map is loaded, select the address and jump to the parent page

Since my requirement is to jump to the parent page after input, the form of the parent page will be refreshed, so a cache process is done.

loadiframe() {
  let iframe = document.getElementById('getAddress').contentWindow;
  iframe.postMessage('hello', 'https://m.amap.com/picker/');
  window.addEventListener("message", function (e) {
  if (e.data.command != "COMMAND_GET_TITLE") {
  //Implement business code let a=this.$route.query.id
  let are = this.$route.query.are
  let address = e.data.address
  let location = e.data.location
  let name = e.data.name
  this.$router.push({
  path: '/addressFill',
    query: {address:address,location:location,name:name,id:a,are:are}
  })

  }

  }.bind(this), false);
},

5. Cache Processing

When jumping to the sub-map selection page, the form data is stored in the cache. When the sub-page jumps back, the current page form data is not refreshed.

localStorage.setItem('names',this.names);
localStorage.setItem('phone',this.phone);
localStorage.setItem('addressDetail',this.addressDetail);
localStorage.setItem('postalCode',this.postalCode);
localStorage.setItem('checked',this.checked);

6. Clear the cache

Caching solves the problem of a page returning no data, but at the same time, the cache of this page will always exist. Therefore, verification is performed when returning the current page, and the cache is cleared.

let addressDetail = localStorage.getItem('addressDetail');

if(addressDetail!=undefined&&addressDetail!=null&&addressDetail!=""){
  this.addressDetail=addressDetail
  localStorage.removeItem("addressDetail");
}else{
  this.addressDetail=""
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Best Practices for Developing Amap Applications with Vue
  • vue+Amap realizes map search and click positioning operation
  • Implementation of Amap introduction and trajectory drawing in Vue
  • Vue introduces the implementation code of AMap Gaode Map
  • Do you know how to use native Amap in Vue?

<<:  mysql: [ERROR] unknown option '--skip-grant-tables'

>>:  Detailed tutorial on uploading and configuring jdk and tomcat on linux

Recommend

Sample code for implementing Google third-party login in Vue

Table of contents 1. Developer Platform Configura...

zabbix custom monitoring nginx status implementation process

Table of contents Zabbix custom monitoring nginx ...

Detailed explanation of the basic commands of Firewalld firewall in Centos7

1. Basics of Linux Firewall The Linux firewall sy...

Nginx 502 Bad Gateway Error Causes and Solutions

I have encountered the Nginx 502 Bad Gateway erro...

Detailed explanation of the execution principle of MySQL kill command

Table of contents Kill instruction execution prin...

Application example tutorial of key in Vue page rendering

introduction During the front-end project develop...

Vue method to verify whether the username is available

This article example shares the specific code of ...

An example of how to quickly deploy web applications using Tomcat in Docker

After learning the basic operations of Docker, we...

Analysis of the principle of Rabbitmq heartbea heartbeat detection mechanism

Preface When using RabbitMQ, if there is no traff...

Key knowledge summary of Vue development guide

Table of contents Overview 0. JavaScript and Web ...