How to Make a Map Using OpenLayers 3
Part 1 of 2:
Installing and Setting Up OpenLayers
- Open or create your webpage. If you don't already have a webpage you want to insert the map into, you can use the following HTML5 template; save it as 'map_page.html' :
<html lang="en"> <head> <meta charset="utf-8"> <title>My OpenLayers 3 Maptitle> head> <body> body> html>
- Include OpenLayers. Your webpage will need to include the OpenLayers library JavaScript file. To do this, paste the following line of code into your HTML file, inside the
area, on a new line below the
:
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol-debug.js" type="text/javascript">script>
- Add an HTML map container. OpenLayers will display the map inside an HTML element, so you need to provide one for it. Paste the following line of markup inside the
:
<div id="map" class="map">div>
- Style the map container. You need to set how large the map will be when it is displayed, and you should use CSS to do this. You can add the following to the document
:
<style> .map { height: 500px; width: 700px; } style>
Part 2 of 2:
Creating the Map
- Create the map object. To start writing the JavaScript code that sets up the map, you'll need to add a
area to the
after the map container
div
. Also inside of this, you can create the map object by callingnew ol.Map()
like this:<script type='text/javascript'> var map = new ol.Map({ }); </script>
- Set the map's target HTML element. OpenLayers will render (display) the map inside an HTML element such as a
div
or ap
. The value that you must pass to the setTarget() function is simplyid
of the element, which we set to 'map':map.setTarget('map');
- Set the map's view. An OpenLayers map's view is what manages what part of of the map you see. Create a new view object that centers on the coordinates [0, 0] (off the coast of central Africa):
Note: Unlike the Leaflet mapping library, OpenLayers 3 deals with coordinates in the format [longitude, latitude].var view = new ol.View({ center: [0, 0], zoom: 1 }) map.setView(view);
- Add a tile layer. OpenLayers maps have layers which are stacked on top of one another, and are what you actually see in a map. There are three types of layers: Image, Tile and Vector. Tile layers are are the standard 'base' layers, and can come from map providers like Google Maps, MapQuest, or OpenStreetMaps. Each layer has a source, which tells OpenLayers where the layer information is coming from.
var tile_layer = new ol.layer.Tile({ source: new ol.source.OSM({layer: 'osm'}) }) map.addLayer(tile_layer);
- Set the zoom level. In OpenLayers, maps can be 'zoomed in' to one of several levels, from 0 (the most zoomed-out) to around 20 (the most zoomed in). The zoom level is set on the view object, so we have to get that and set its zoom:
map.getView().setZoom(2);
- Check your code. Below is what your webpage code should look like:
doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>My OpenLayers 3 Map</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol-debug.js" type="text/javascript"></script> <style> .map { height: 500px; width: 700px; } </style> </head> <body> <div id="map" class="map"></div> <script type='text/javascript'> var map = new ol.Map({ }); map.setTarget('map'); var view = new ol.View({ center: [0, 0], zoom: 1 }) map.setView(view); var tile_layer = new ol.layer.Tile({ source: new ol.source.OSM({layer: 'osm'}) }) map.addLayer(tile_layer); map.getView().setZoom(2); </script> </body> </html>
- The finished map.
4 ★ | 1 Vote
You should read it
- How to view RAM RAM on a PC
- Instructions to turn off Protected View feature in Office 2013
- What is Protected View? Turn on and off the Protected View feature in Office 2016
- VIEW in SQL Server
- This is how to view the cache of any Web site
- How to Choose Point of View in Storytelling
- How to set the right to view Zalo Logs
- Steps to use Outline View in Microsoft Word
May be interested
- 6 How to prepare delicious oats for the elderly and the sickthe following article, tipsmake.com would like to introduce you 6 ways to make delicious, nutritious bird's nest for the elderly and the sick.
- A guide to making delicious and beautiful chocolates at homehere's how to make chocolate both simple and economical! try one of the following two methods to make your own beautiful chocolate candy at home!
- Ways to make smart mouse traps with simple household itemsdomestic insects and rodents not only cause economic losses but also cause the spread of many dangerous diseases. how to kill rats without harm or cost too much? please refer to a few ways to make smart mouse traps from simple household items below! surely the cousins will be terrified.
- Minecraft medicine recipe, how to make Minecraft medicineapparently players can make themselves a 'doctor' in minecraft by learning about potion recipes that can help you avoid danger or give you an advantage in certain situations.
- How to make Vietcombank Online cardnow, you can make vietcombank online card quickly and easily via this bank's website. with just a few simple steps as instructed below, you will have a convenient vietcombank atm card right away.
- 4 ways to make delicious cucumber saladcucumber salad is a delicious dish, easy to eat on hot days. here are 4 ways to make delicious cucumber salad, simple not only delicious but also help to lose weight, beautiful skin, please refer.
- How to make delicious meat stock shipbraised pork with rich flavor, greasy, fragrant quail eggs, thick viscous water to eat very rice, especially in the cold season. the following article will guide you how to cook delicious warehouse meat stock, right.
- Things that can make you look older than your actual ageaging is inevitable, but there are certain habits that can speed up the process without you even realizing it.
- How to make Techcombank Online cardinstead of having to go to the tecombank transaction office or branch to make a bank card, you can choose to make techcombank online card at home.
- Making homemade mango ice cream in 4 simple ways, no machine requiredhere are 4 simple ways to make mango ice cream, no machine, anyone can make at home to have a delicious fruit ice cream for family on summer day.