OpenLayers 3 Basic Example

by kalifa17

HTML

<link rel="stylesheet" href="http://openlayers.org/en/v3.2.1/css/ol.css">
<script src="http://openlayers.org/en/v3.2.1/build/ol.js"></script>
<title>OpenLayers 3 example</title>
<body>
  <h2>My Map</h2>
  <div id="map" class="map"></div> 
  <button onclick="changeZoom()">Change Zoom</button>
  <button onclick="changeBack()">Change Back</button>
</body>

CSS

.map {
  height: 400px;
  width: 100%;
}

JavaScript

var map = new ol.Map({
   target: 'map',
   layers: [
     new ol.layer.Tile({
       source: new ol.source.MapQuest({layer: 'sat'})
     })
   ],
   view: new ol.View({
     center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
     zoom: 5,
      maxZoom: 7,
      minZoom: 0,
   })
});


function changeZoom(){
	map.setView(new ol.View({
  		center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
      zoom: 11,
      maxZoom: 11,
      minZoom: 0,
  }));
}

function changeBack(){
	map.setView(new ol.View({
  		center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
      zoom: 5,
      maxZoom: 7,
      minZoom: 0,
  }));
}