简单标记图标示例

此示例展示如何设置标记的图标。

关键代码

	//添加一个红色的图标
    var image = '//api.go2map.com/maps/images/v2.0/S20.png';
    var myLatLng = new sogou.maps.LatLng(39.991577,116.366495);
    var beachMarker = new sogou.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image
    });

代码解析

设置图标路径。在创建标记时使用指定图标。

	//添加一个红色的图标
    var image = '//api.go2map.com/maps/images/v2.0/S20.png';
    var myLatLng = new sogou.maps.LatLng(39.991577,116.366495);
    var beachMarker = new sogou.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image
    });

完整代码

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>搜狗地图 JavaScript API示例: 简单标记图标示例</title>
<style type="text/css">
html {height: auto;}
body {height: auto;margin: 0;padding: 0;}
#map_canvas {width:1000px;height: 500px;position: absolute;}
@media print {#map_canvas {height: 950px;}}
</style>
<script type="text/javascript" src="//api.go2map.com/maps/js/api_v2.5.1.js"></script>
<script type="text/javascript">
 /**
  * 简单标记图标示例
  */
  function initialize() {
    var myOptions = {
      zoom: 4,
      center: new sogou.maps.LatLng(39.991577,116.366495),
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    }
    var map = new sogou.maps.Map(document.getElementById("map_canvas"),
                                  myOptions);
	//添加一个红色的图标
    var image = '//api.go2map.com/maps/images/v2.0/S20.png';
    var myLatLng = new sogou.maps.LatLng(39.991577,116.366495);
    var beachMarker = new sogou.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image
    });
  }
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas"></div>
</body>
</html>

运行代码