最近需要在百度地圖上做一個自定義icon,並且有標注,翻看了一下api,自定義icon和標注是分開的,兩者放一起效果如下:

相關代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自定義Marker圖標</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<style>
body,
html,
#container {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
font-family: "微軟雅黑";
}
</style>
<script src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=您的密鑰"></script>
</head>
<body>
<div id="container"></div>
</body>
</html>
<script type="text/javascript">
var map = new BMapGL.Map('container');
var point = new BMapGL.Point(116.404, 39.915);
map.centerAndZoom(point, 15);
// 創建小車圖標
var myIcon = new BMapGL.Icon("/jsdemo/img/car.png", new BMapGL.Size(52, 26));
// 創建Marker標注,使用小車圖標
var pt = new BMapGL.Point(116.417, 39.909);
var marker = new BMapGL.Marker(pt, {
icon: myIcon
});
// 創建文本標注對象
var labelopts = {
position: pt, // 指定文本標注所在的地理位置
offset: new BMapGL.Size(-5, 10) // 設置文本偏移量
};
var label = new BMapGL.Label('測試標注', labelopts);
label.setStyle({
color : "#fff",
fontSize : "14px",
backgroundColor :"0.05",
backgroundColor:"#84C1FF",
border :"0",
fontWeight :"bold"
});
map.addOverlay(label);
map.addOverlay(marker);
</script>
