search
HomeWeb Front-enduni-appHow to open the map in uniapp
How to open the map in uniappApr 20, 2023 pm 01:52 PM

With the development of the intelligent era, map applications have become an indispensable part of people's lives. The advancement of map technology also makes it easier for developers to use maps to provide better services. Among them, uniapp, as a cross-platform development framework, also supports the development and use of maps. But for some beginners, they may face the problem of how to open the map in uniapp. This article will introduce you in detail how to open the map in uniapp.

1. Use the map component that comes with uniapp

Uniapp provides the uniMap component, which can more easily display the map in uniapp. The specific usage is as follows:

1. Introduce the uniMap component

<template>
   <view>
      <uni-maps></uni-maps>
   </view>
</template>

<script>
   export default{
       data(){
            return{
                 longitude: &#39;116.362887&#39;,
                 latitude: &#39;39.960143&#39;,
                 markers:[
                     {
                           id:1,
                           longitude:&#39;116.362887&#39;,
                           latitude:&#39;39.960143&#39;,
                           title:&#39;北京站&#39;,
                     },
                     {
                           id:2,
                           longitude:&#39;116.407894&#39;,
                           latitude:&#39;39.904265&#39;,
                           title:&#39;天安门&#39;,
                     },
                 ]
            }
        }
    }
</script>

As you can see, the uni-maps component needs to be introduced in the code and the three parameters of longitude, latitude, and markers are passed in. Among them, longitude and latitude represent the longitude and latitude of the map, and markers are optional parameters used to display markers on the map.

2. Write the map style

In the above code, only the map markers and coordinates are displayed. If you want to beautify the appearance of the map, you need to write some styles to control it. The following is a simple implementation method:

.uni-maps{
    height:800rpx;
    width: 100%;
}

.uni-maps /deep/ .xm-map-scale {
    background-color: #fff;
    color: #666;
}

.uni-maps /deep/ .xm-map-timesvg{
    width:18px;
}

It should be noted that the height of the map container must be set, otherwise it cannot be displayed normally.

2. Use third-party map plug-ins

uniapp also supports the use of third-party map plug-ins. Taking Amap as an example, you need to configure relevant parameters in main.js first:

import Vue from 'vue';
import App from './App';
import store from './store';
import {
   router,
   // #ifdef APP-PLUS
   mpvueAndroidBackEvent,
   // #endif
   RouterMount
} from './router';
import request from '@/common/request';

import index from '@/pages/index/index.vue';

Vue.config.productionTip = false;
App.mpType = 'app';

Vue.prototype.$http = request;

let amapPlugin = requirePlugin('amapPlugin');

Vue.prototype.$amapPlugin = amapPlugin;

Vue.component('index', index);

// #ifdef APP-PLUS
mpvueAndroidBackEvent(router, () => {
   console.log('android-hardware-back-event');
   let pages = getCurrentPages();
   console.log('pages: ', pages);
   if (pages.length > 1) {
     router.back(-1);
   } else {
     router.push('/pages/tabbar/index');
     return;
   }
});
// #endif

// #ifdef H5
router.onReady(() => {
   if (router.app.$route.path === '/') {
      router.push('/pages/tabbar/index');
   }
});
// #endif

RouterMount(App, router, '#app');

Use the amapPlugin plug-in in the vue component:

<template>
   <view>
      <view>
         <map></map>
      </view>
   </view>
</template>
<script>
   export default {
      data() {
         return {
            id: &#39;map&#39;,
            markersList: [
               {
                  id: 0,
                  iconPath: &#39;../../static/images/icon_location.png&#39;,
                  longitude: &#39;&#39;,
                  latitude: &#39;&#39;,
                  width: 40,
                  height: 40
               }
            ]
         }
      },
      onReady() {
         let vm = this;
         let amapPlugin = vm.$amapPlugin.createAmap({
            key: &#39;your amap key&#39;,//此处填写你的高德地图key
            version: &#39;&#39;,
         });
         wx.getLocation({
            type: &#39;gcj02&#39;,
            success: res => {
               console.log(res);
               if (res.longitude && res.latitude) {
                  vm.markersList[0].longitude = res.longitude;
                  vm.markersList[0].latitude = res.latitude;
                  let marker = vm.markersList[0];
                  let cpoint = [res.longitude, res.latitude];
                  amapPlugin.getRegeo({
                     location: cpoint.join(),
                     success: function (data) {
                        marker.title = data[0].name;
                        marker.address = data[0].desc;
                        vm.markersList = [marker];
                     },
                     fail: function (info) {
                        console.log(info);
                     },
                  });
               }
            }
         })
      }
   }
</script>
<style>
   .map {
      height: 100%;
      overflow: hidden;
   }

   map,
   image,
   textarea,
   scroll-view {
      width: 100%;
      height: 100%;
   }
</style>

It should be noted that using a third-party map plug-in It needs to be configured in main.js and called using the createAmap method in the .vue component.

Summary:

There are two ways to use maps in uniapp, one is to use the map component that comes with uniapp, and the other is to use a third-party map plug-in. Choose which method to use based on your needs. No matter which method is used, you need to have a certain understanding of the map plug-in first and be familiar with the calling method of the map plug-in in order to develop better.

The above is the detailed content of How to open the map in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I handle local storage in uni-app?How do I handle local storage in uni-app?Mar 11, 2025 pm 07:12 PM

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

How do I make API requests and handle data in uni-app?How do I make API requests and handle data in uni-app?Mar 11, 2025 pm 07:09 PM

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

How do I manage state in uni-app using Vuex or Pinia?How do I manage state in uni-app using Vuex or Pinia?Mar 11, 2025 pm 07:08 PM

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

How do I use uni-app's geolocation APIs?How do I use uni-app's geolocation APIs?Mar 11, 2025 pm 07:14 PM

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

How do I use uni-app's social sharing APIs?How do I use uni-app's social sharing APIs?Mar 13, 2025 pm 06:30 PM

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

How do I use uni-app's easycom feature for automatic component registration?How do I use uni-app's easycom feature for automatic component registration?Mar 11, 2025 pm 07:11 PM

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.

How do I use preprocessors (Sass, Less) with uni-app?How do I use preprocessors (Sass, Less) with uni-app?Mar 18, 2025 pm 12:20 PM

Article discusses using Sass and Less preprocessors in uni-app, detailing setup, benefits, and dual usage. Main focus is on configuration and advantages.[159 characters]

How do I use uni-app's uni.request API for making HTTP requests?How do I use uni-app's uni.request API for making HTTP requests?Mar 11, 2025 pm 07:13 PM

This article details uni.request API in uni-app for making HTTP requests. It covers basic usage, advanced options (methods, headers, data types), robust error handling techniques (fail callbacks, status code checks), and integration with authenticat

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment