天天看點

OpenLayers3學習(二)--滑鼠移動事件

在Web地圖應用開發過程中,最常見的事件之一就是使用滑鼠在地圖上移動,這裡我們利用(一)中的地圖服務,在此基礎上建立一個滑鼠移動的事件,代碼如下:

<!DOCTYPE html>
<html>
  <head>
    <title>Tiled ArcGIS MapServer</title>
    <link rel="stylesheet" href="http://openlayers.org/en/v3.17.1/css/ol.css" target="_blank" rel="external nofollow"  type="text/css">
    <script src="http://openlayers.org/en/v3.17.1/build/ol.js"></script>
  </head>
  
  <style>
  	.map
  	{
  		width: 1000px;
  		height: 700px;
  	}
  </style>
  
  <body>
    <div id="map" class="map"></div>
    <div id="mouse-position"></div>
    <script>
      var url = 'http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/' +
          'Specialty/ESRI_StateCityHighway_USA/MapServer';

      var layers = [
        new ol.layer.Tile({
          source: new ol.source.OSM()
        }),
        new ol.layer.Tile({
          extent: [-13884991, 2870341, -7455066, 6338219],
          source: new ol.source.TileArcGISRest({
            url: url
          })
        })
      ];
      
      //mousePositionControl一定要放在map對象建立之前建立
       var mousePositionControl = new ol.control.MousePosition({
        coordinateFormat: ol.coordinate.createStringXY(4),
        className: 'custom-mouse-position',
        target: document.getElementById('mouse-position'),
        undefinedHTML: ' '
      });
      
      var map = new ol.Map({
        layers: layers,
        target: 'map',
        view: new ol.View({
          center: [-10997148, 4569099],
          zoom: 4
        }),
        controls:ol.control.defaults().extend([mousePositionControl])
        });
      
     
      
    </script>
  </body>
</html>