uniApp使用高德地图api
1,在自己项目中的/src/common/js中的weixin.js写入,没有就新建文件,(具体目录因自己项目而议)
export const weixin = {
getLocation: function (callback) {
let option = {
type: 'gcj02',
success: function (res) {
if (callback && callback instanceof Function) {
callback(res);
}
}
}
wx.getLocation(option);
},
openLocation: function(data){
wx.openLocation({
longitude: Number(data.longitude),
latitude: Number(data.latitude),
name: data.name,
address: data.address
})
},
}
2,在自己项目中的/src/common/js中的新建aMap.js文件,(具体目录因自己项目而议)
export const MapLoader = function () {
return new Promise((resolve, reject) => {
if (window.AMap) {
resolve(window.AMap)
} else {
var script = document.createElement('script')
script.type = 'text/javascript'
script.async = true
script.src = 'http://webapi.amap.com/maps?v=1.4.8&callback=initAMap&key=你的key值'
script.onerror = reject
document.head.appendChild(script)
}
window.initAMap = () => {
resolve(window.AMap)
}
})
}
export const getAddress = function (longitude, latitude) {
MapLoader().then(AMap => {
AMap.plugin('AMap.Geocoder', function () {
var geocoder = new AMap.Geocoder()
var lnglat = [longitude, latitude]
geocoder.getAddress(lnglat, function (status, result) {
if (status === 'complete' && result.info === 'OK') {
return result
}
})
})
},
e => { console.log('地图加载失败', e) }
)
}
export const getLocation = function (site) {
return new Promise((resolve)=>{
MapLoader().then(AMap => {
AMap.plugin('AMap.Geocoder', function () {
var geocoder = new AMap.Geocoder()
geocoder.getLocation(site, function (status, result) {
if (status === 'complete' && result.info === 'OK') {
resolve(result)
}
})
})
},
e => {
console.log('地图加载失败', e);
resolve(false)
}
)
})
}
export const getCircum = function (newLng,newLat) {
return new Promise((resolve)=>{
MapLoader().then(AMap => {
AMap.plugin('AMap.Geocoder', function () {
var map = new AMap.Map("container", {
resizeEnable: true
});
AMap.service(["AMap.PlaceSearch"], function() {
var placeSearch = new AMap.PlaceSearch({
type: '',
pageSize: 6,
pageIndex: 1,
city: "",
citylimit: false,
map: map,
panel: "panel",
autoFitView: true
});
var cpoint = [newLng, newLat];
placeSearch.searchNearBy('', cpoint, 200);
AMap.event.addListener(placeSearch, 'complete', onComplete)
function onComplete (data) {
console.log('定位成功信息')
}
});
})
},
e => {
console.log('地图加载失败', e);
resolve(false)
}
)
})
}
3,在要使用到高德的api文件中使用:
import { getWxOpenId, weixin } from '@/common/js/weixin.js'
import { getLocation } from '@/common/js/AMap.js';
methods: {
openLocation(item, index){
let that = this;
getLocation(item).then(res=>{
if(res && res.geocodes.length >0){
let lng = res.geocodes[0].location.lng;
let lat = res.geocodes[0].location.lat;
if(index == 0) {
weixin.openLocation({
longitude: lng,
latitude: lat,
name: res.geocodes[0].formattedAddress,
address: res.geocodes[0].formattedAddress
})
}else{
that.navigateTo('./entrepotAmap?lng=' + lng + '&lat=' + lat);
}
}
});
},
}
4,新建放地图周边的vue文件entrepotAmap.vue:(这一步看自己项目需求写,没有可以不管) html:
<template>
<view id="entrepotAmap">
<view class="back" @click="backPage()"></view>
<view id="container"></view>
<view id="panel"></view>
</view>
</template>
js:
<script>
import { getCircum } from '@/common/js/AMap.js';
export default {
data() {
return {
aMapLng: '',
aMapLat: '',
}
},
mounted() {
this.aMapLng = this.$route.query.lng;
this.aMapLat = this.$route.query.lat;
this.getLocation();
},
methods: {
getLocation () {
getCircum(this.aMapLng,this.aMapLat).then(res=>{
console.log('');
});
},
backPage() {
uni.navigateBack({});
},
}
}
</script>
css:
<style lang="scss" scoped>
#entrepotAmap{
width: 750upx;
box-sizing: border-box;
background: #fff;
height: 100vh;
overflow: hidden;
.back {
position: absolute;
top: 26upx;
left: 16upx;
width: 24upx;
height: 32upx;
background: url('../../static/images/home/2.png') no-repeat center /
100%;
transform: rotate(180deg);
z-index: 999;
}
#container{
width: 100%;
height: 100vh;
}
#panel {
position: absolute;
background-color: white;
max-height: 35%;
overflow-y: auto;
top: 10upx;
right: 10upx;
width: 560upx;
font-size: 24upx;
}
}
</style>
|