How to introduce Gaode map into the mini program? This article will introduce to you how to use Amap in the WeChat applet. I hope it will be helpful to you!
Obtain the Amap user key
If you don’t have a key, you need to apply first and enter the Amap development platformlbs.amap.com/, there are detailed steps in Development Guide-> Obtain key, and you can view the key we created in Console-> Application Management-> My Application. [Related learning recommendations: 小program development tutorial]
We can encapsulate the key so that we don’t have to look for it every time. In lib Create a new config.js file in the folder
1 2 3 4 | var config = {
key: "你的key"
}
module.exports.config = config;
|
Copy after login
Import the js and key of Amap in js to call the Amap map api
1 2 | var amapFile = require(& #39;../../lib/amap-wx.130.js'); //高德js
var config = require(& #39;../../lib/config.js'); //引用我们的配置文件
|
Copy after login
Get the current location
Create a Gaode map instance and name it myAmapFun
1 2 3 4 | var key = config.config.key;
var myAmapFun = new amapFile.AMapWX({
key: key
});
|
Copy after login
Call the getRegeo method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | myAmapFun.getRegeo({
success: (data) => {
let textData = {};
textData.name = data[0].name;
textData.desc = data[0].desc
this .setData({
textData: textData,
longitude: data[0].longitude,
latitude: data[0].latitude,
markers: [{
latitude: data[0].latitude,
longitude: data[0].longitude,
height: 30,
width: 35,
iconPath: & #39;../../imgs/locationIcon/siteA brief analysis of how to introduce Amap into mini programs'
}]
})
},
fail: function (info){
console.log( "get Location fail" );
}
});
|
Copy after login
We can look at the successfully output data, and we can take the information according to our own needs
Display the map in the wxml file. The width is 100%, the height is 400px, scale
is the zoom ratio of the map
1 2 3 4 5 6 7 8 | <view class= "map_container" >
<map class= "map" name= "" longitude= "{{longitude}}" latitude= "{{latitude}}" scale= "16" show-location= "true" markers= "{{markers}}" >
</map>
</view>
<view class= "map_text" >
<text class= "h1" >{{textData.name}}</text>
<text>{{textData.desc}}</text>
</view>
|
Copy after login
Red The marked points are the data of markers; the blue marked points are displayed with show-location="true", but there is no real device preview.
Get nearby points , only take the first ten
1 2 3 4 5 6 7 8 9 10 11 12 | data: {
# 当前位置经度
longitude: "" ,
# 当前位置纬度
latitude: "" ,
# 获取位置的标记信息
markers: [],
# 获取位置的位置信息
poisdatas : [],
# 简单展示信息使用的
textData: {}
}
|
Copy after login
Call the getPoiAround interface of Amap to obtain nearby information based on keywords
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | get_current_PoiAround(){
var key = config.config.key;
var myAmapFun = new amapFile.AMapWX({
key: key
});
myAmapFun.getRegeo({
success: (data) => {
let textData = {};
textData.name = data[0].name;
textData.desc = data[0].desc
this .setData({
textData: textData,
longitude: data[0].longitude,
latitude: data[0].latitude,
})
},
fail: function (info){
console.log( "get Location fail" );
}
});
myAmapFun.getPoiAround({
iconPath: & #39;../../icon/keshan/blue.svg',
iconPathSelected: & #39;../../icon/keshan/blue.svg',
querykeywords: & #39;购物',
querytypes: & #39;060100',
success: (data) => {
const markers = data.markers;
const poisdatas = data.poisData;
let markers_new = []
markers.forEach((item, index) => {
if ( index >= 10 ){
return ;
}
markers_new.push({
id: item.id,
width: item.width,
height: item.height,
iconPath: item.iconPath,
latitude: item.latitude,
longitude: item.longitude,
callout: {
padding: 2,
fontSize: 15,
bgColor: "#f78063" ,
color: & #39;#ffffff',
borderRadius: 5,
display: & #39;BYCLICK',
content: poisdatas[index].name
}
})
})
this .setData({
markers: markers_new,
poisdatas: poisdatas
})
},
fail: function (info){
wx.showModal({title:info.errMsg})
}
})
},
|
Copy after login
Call the getPoiAround interface to return a successful result
bindmarkertap activates the makertap icon click event and changes the content in map_text
1 2 3 4 5 6 7 8 9 10 | < view class = "map_container" >
< map class = "map" id = "map" name = "" longitude = "{{longitude}}" latitude = "{{latitude}}" scale = "16" show-location = "true" markers = "{{markers}}" bindmarkertap = "makertap" >
</ map >
</ view >
< view class = "map_text" >
< text class = "h1" >{{textData.name}}</ text >
< text wx:if = "{{textData.distance != null}}" >{{textData.distance}}m</ text >
< text >{{textData.desc}}</ text >
</ view >
|
Copy after login
makertap activates showMarkerInfo to display mark point information, changeMarkerColor Change the color of the marker point
1 2 3 4 5 | makertap(e) {
var id = e.detail.markerId;
this .showMarkerInfo(id);
this .changeMarkerColor(id);
},
|
Copy after login
Didn’t we say that poisdatas stores the location information of the point? Once we get the ID, we can take it out and save it in textData to display
1 2 3 4 5 6 7 8 9 10 11 | showMarkerInfo(i) {
const {poisdatas} = this .data;
this .setData({
textData: {
name: poisdatas[i].name,
desc: poisdatas[i].address,
distance: poisdatas[i].distance
}
})
},
|
Copy after login
If it is the clicked position Just replace iconPath with orange.svg, and the rest are blue.svg, and set the clicked bubble display to display ('ALWAYS'), and then save the modified data again
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | changeMarkerColor(index) {
let {markers} = this .data;
for ( var i = 0; i < markers.length; i++) {
if (i == index) {
markers[i].iconPath = "../../icon/keshan/orange.svg" ;
markers[i].callout.display = & #39;ALWAYS'
} else {
markers[i].iconPath = "../../icon/keshan/blue.svg" ;
markers[i].callout.display = & #39;BYCLICK'
}
}
this .setData({
markers: markers
})
},
|
Copy after login
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of A brief analysis of how to introduce Amap into mini programs. For more information, please follow other related articles on the PHP Chinese website!