赞
踩
1、首先引入amap-wx.130.js文件,在项目中可以新建文件夹common,在下面引入该js文件
2、在使用的页面引入该js,
- <template>
- <view></view>
- </template>
- <script>
- import amap from '@/common/amap-wx.130.js'
- export default {
- data() {
- return {
- amapPlugin:null
- }
- }
- }
3、获取定位信息(注意先需要申请高德地图的key)申请方式如下连接
- <template>
- <view></view>
- </template>
- <script>
- import amap from '@/common/amap-wx.130.js'
- export default {
- data() {
- return {
- amapPlugin:null
- }
- },
- async onLoad(item) {
- this.amapPlugin = new amap.AMapWX({
- key: '你的key值'
- });
- var that = this
- this.amapPlugin.getRegeo({
- success: (data) => {
- console.log('小程序获取定位成功')
- console.log(222, data)
- // 经度
- that.data3.lng = data[0].longitude;
- // 纬度
- that.data3.lat = data[0].latitude;
- // 省
- that.data3.province = data[0].regeocodeData.addressComponent.province
- // 市
- that.data3.city = data[0].regeocodeData.addressComponent.city
- // 区
- that.data3.district = data[0].regeocodeData.addressComponent.district
-
- uni.setStorageSync('longitude', that.data3.lng)
- uni.setStorageSync('latitude', that.data3.lat)
- uni.setStorageSync('longitude2', that.data3.lng)
- uni.setStorageSync('latitude2', that.data3.lat)
- uni.setStorageSync('province', that.data3.province)
- uni.setStorageSync('city', that.data3.city)
- uni.setStorageSync('district', that.data3.district)
- },
- fail: function(res) {
- console.log(res)
- }
- });
- }
- }
- <template>
- <view></view>
- </template>
- <script>
- import amap from '@/common/amap-wx.130.js'
- export default {
- data() {
- return {
- amapPlugin:null
- }
- },
- methods:{
- async chooseSpotApp() {
-
- let that = this
- uni.getLocation({
- type: 'gcj02',
- isHighAccuracy: true,
- geocode: true,
- success: function(res) {
- console.log(res)
- // 经度
- that.data3.lng = res.longitude;
- // 纬度
- that.data3.lat = res.latitude;
- // 省
- that.data3.province = res.address.province
- // 市
- that.data3.city = res.address.city
- // 区
- that.data3.district = res.address.district
-
- uni.setStorageSync('longitude', that.data3.lng)
- uni.setStorageSync('latitude', that.data3.lat)
- uni.setStorageSync('longitude2', that.data3.lng)
- uni.setStorageSync('latitude2', that.data3.lat)
- uni.setStorageSync('province', that.data3.province)
- uni.setStorageSync('city', that.data3.city)
- uni.setStorageSync('district', that.data3.district)
- let key = '替换你的key'; //高德地图key
- uni.request({
- url: 'https://restapi.amap.com/v3/geocode/regeo?location=' + that
- .longitude + ',' + that.latitude + '&key=' + key,
- success: (res) => {
- console.log('高德地图API接口返回信息', res.data.regeocode.addressComponent)
-
- uni.setStorageSync('sheng', res.data.regeocode.addressComponent
- .province)
- },
- fail: (error) => {
- console.log(error)
- }
- })
- },
- fail(res) {
- console.log(111, res)
- }
- });
- },
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。