1. 准备
2. 创建地图
<script charset="utf-8" src="https://map.qq.com/api/gljs?v=1.exp&key=此处填入腾讯位置服务密钥"></script>
<body onload="initMap()">
<div id="container"></div>
<script type="text/javascript">
function initMap() {
var center = new TMap.LatLng(24.594221617, 117.9752365);
var ne = new TMap.LatLng(24.599587,117.980751);
var sw = new TMap.LatLng(24.588856,117.969722);
var latLngBounds = new TMap.LatLngBounds(sw, ne);
var map = new TMap.Map("container", {
rotation: 0,
pitch: 45,
zoom: 14,
center: center,
boundary: latLngBounds,
showControl: false,
baseMap: {
type: 'vector',
features: [
'base',
'building3d',
'label'
]
}
});
}
</script>
</body>
- 设置了边界范围后,拖拽、缩放等操作无法将地图移动至边界外,默认为null
3. 添加标记和文本
var position1 = new TMap.LatLng(39.916527, 116.397128);
var marker = new TMap.MultiMarker({
id: "marker-layer",
map: map,
styles: {
"marker": new TMap.MarkerStyle({
"width": 25,
"height": 35,
"src": "https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png"
})
},
geometries: [{
"id": "demo",
"styleId": "marker",
"position": position1,
"properties": {
"title": "marker"
}
}]
});
var label = new TMap.MultiLabel({
id: 'label-layer',
map: map,
styles: {
'label': new TMap.LabelStyle({
'color': '#3777FF',
'size': 20,
'offset': {x: 0, y: 10},
'angle': 0,
'alignment': 'center',
'verticalAlignment': 'top'
})
},
geometries: [{
'id': 'label',
'styleId': 'label',
'position': position1,
'content': '总部',
'properties': {
'title': 'label'
}
}]
});
4. 点击标记,弹出信息窗口
var infoWindow = new TMap.InfoWindow({
map: map,
position: center,
offset: { x: 0, y: -32 }
});
infoWindow.close();
marker.on("click", function (evt) {
infoWindow.open();
infoWindow.setPosition(evt.geometry.position);
infoWindow.setContent(evt.geometry.properties.title);
5. 绘制建筑模型
var path = [[
new TMap.LatLng(24.590890, 117.974330),
new TMap.LatLng(24.592573, 117.972388),
new TMap.LatLng(24.595549, 117.975520),
new TMap.LatLng(24.593895, 117.977430),
]]
var polygon = new TMap.MultiPolygon({
id: 'polygon-layer',
map: map,
styles: {
'polygon': new TMap.ExtrudablePolygonStyle({
'color': 'rgba(0,125,255,0.2)',
'showBorder': true,
'extrudeHeight': 30,
'borderColor': 'rgba(0,125,255,1)'
})
},
geometries: [
{
'id': 'p1',
'styleId': 'polygon',
'paths': path,
}
]
});
6. 绘制路线
- 效果
- 源码
var paths = [
new TMap.LatLng(24.590885, 117.974335),
new TMap.LatLng(24.591480, 117.973638),
new TMap.LatLng(24.592251, 117.972747),
new TMap.LatLng(24.592573, 117.972382),
new TMap.LatLng(24.593188, 117.972972),
new TMap.LatLng(24.595266, 117.975204),
new TMap.LatLng(24.595568, 117.975537),
new TMap.LatLng(24.595188, 117.975987),
];
var green = 'rgba(0, 180, 0, 1)',
red = 'rgba(255, 0, 0, 1)',
yellow ='rgba(204,153, 0, 1)';
var polylineLayer = new TMap.MultiPolyline({
map: map,
styles: {
'style_blue': new TMap.PolylineStyle({
color: '#3777FF',
width: 10,
borderWidth: 0,
showArrow: true,
arrowOptions: {
space: 70
},
lineCap: 'round',
}),
},
geometries: [{
styleId: 'style_blue',
rainbowPaths: [
{
path: [paths[0], paths[1]],
color: yellow,
},
{
path: [paths[1], paths[2]],
color: green,
},
{
path: [paths[2], paths[3]],
color: red,
},
{
path: [paths[3], paths[4]],
color: yellow,
},
{
path: [paths[4], paths[5]],
color: green,
},
{
path: [paths[5], paths[6]],
color: red,
},
{
path: [paths[6], paths[7]],
color: yellow,
}
],
}],
});
7. 规划路径
- 效果
- 源码
window.map = map;
window.drivingFrom = [24.590129, 117.971079];
window.drivingTo = [24.593636, 117.972951];
var url = "https://apis.map.qq.com/ws/direction/v1/driving/";
url += "?from=" + drivingFrom[0] + "," + drivingFrom[1];
url += "&to=" + drivingTo[0] + "," + drivingTo[1];
url += "&output=jsonp&callback=cb";
url += "&key=此处填入腾讯位置服务密钥";
jsonp_request(url);
function jsonp_request(url) {
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
}
function cb(ret) {
var coords = ret.result.routes[0].polyline, pl = [];
var kr = 1000000;
for (var i = 2; i < coords.length; i++) {
coords[i] = Number(coords[i - 2]) + Number(coords[i]) / kr;
}
for (var i = 0; i < coords.length; i += 2) {
pl.push(new TMap.LatLng(coords[i], coords[i + 1]));
}
display_polyline(pl)
var marker = new TMap.MultiMarker({
id: 'marker-layer-driving',
map: map,
styles: {
"start": new TMap.MarkerStyle({
"width": 25,
"height": 35,
"anchor": {x: 16, y: 32},
"src": 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/start.png'
}),
"end": new TMap.MarkerStyle({
"width": 25,
"height": 35,
"anchor": {x: 16, y: 32},
"src": 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/end.png'
})
},
geometries: [{
"id": 'start',
"styleId": 'start',
"position": new TMap.LatLng(drivingFrom[0], drivingFrom[1])
}, {
"id": 'end',
"styleId": 'end',
"position": new TMap.LatLng(drivingTo[0], drivingTo[1])
}]
});
}
function display_polyline(pl) {
var polylineLayer = new TMap.MultiPolyline({
id: 'polyline-layer',
map: map,
styles: {
'style_blue': new TMap.PolylineStyle({
'color': '#3777FF',
'width': 8,
'borderWidth': 5,
'borderColor': '#FFF',
'lineCap': 'round',
})
},
geometries: [
{
'id': 'pl_1',
'styleId': 'style_blue',
'paths': pl
}
]
});
}