加载卫星地图并作标记

此示例展示的是如何将地图设成卫星地图,同时创建标记,点击标记弹出信息窗

关键代码

map.setMapTypeId(sogou.maps.MapTypeId.SATELLITE);

var marker = new sogou.maps.Marker({map: map, position:
map.getCenter()});
var infowindow = new sogou.maps.InfoWindow();
infowindow.setContent('测试');
sogou.maps.event.addListener(marker, 'click', function() {
   infowindow.open(map, marker);
});
			

代码解析

首先要先创建一个地图,并将地图设置成卫星地图。

var map = new sogou.maps.Map(document.getElementById("map_canvas"), {//创建地图
        scaleControl: true});
map.setCenter(new sogou.maps.Point(12955474,4825109),10);//设定中心
map.setMapTypeId(sogou.maps.MapTypeId.SATELLITE);//将地图设成卫星地图
			

接下来创建一个标记。

var marker = new sogou.maps.Marker({map: map, position:
map.getCenter()});
			

创建一个信息窗实例。指定信息窗显示的内容。侦听标记点击事件

var infowindow = new sogou.maps.InfoWindow();
infowindow.setContent('测试');
sogou.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
			

完整代码

<!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 init() {
      var map = new sogou.maps.Map(document.getElementById("map_canvas"), {
        scaleControl: true});
      map.setCenter(new sogou.maps.Point(12955474,4825109),10);
      map.setMapTypeId(sogou.maps.MapTypeId.SATELLITE);
 
      var marker = new sogou.maps.Marker({map: map, position:
        map.getCenter()});
      var infowindow = new sogou.maps.InfoWindow();
      infowindow.setContent('<b>测试</b>');
      sogou.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
      });
    }
</script> 
</head> 
<body onload="init()">
  <div id="map_canvas" style="width: 500px; height: 400px;"></div> 
</body> 
</html> 
			

运行代码