1. Vector MapVector graphics use straight lines and curves to describe graphics. The elements of these graphics are points, lines, rectangles, polygons, circles, arcs, etc., which are all obtained by calculating mathematical formulas. Since vector graphics can be obtained through formula calculation, the file size of vector graphics is generally small. The biggest advantage of vector graphics is that they will not be distorted no matter they are enlarged, reduced or rotated. There are a large number of applications in maps, which is a very important component of map data. In order to facilitate storage, transmission and use, vector maps are expressed in certain formats, such as the common 2. Loading vector maps using GeoJson format1. Project Structure 2. map.geojson
3. map.html <!Doctype html> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html;charset=utf-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'> <meta content='always' name='referrer'> <title>OpenLayers 3: Loading vector maps</title> <link href='ol.css ' rel='stylesheet' type='text/css'/> <script type='text/javascript' src='ol.js' charset='utf-8'></script> </head> <body> <div id='map' style='width: 1000px;height: 800px;margin: auto'></div> <script> /** * Create a map */ new ol.Map({ // Set the map layers layers: [ //Create a layer that uses the Open Street Map map source new ol.layer.Tile({ source: new ol.source.OSM() }), //Load a geojson vector map new ol.layer.Vector({ source: new ol.source.Vector({ url: 'geojson/map.geojson', // Map source format: new ol.format.GeoJSON() // Formatting class for parsing vector maps}) }) ], // Set the view to display the map view: new ol.View({ center: [104,30], // Set the map display center to longitude 104 degrees and latitude 30 degrees zoom: 10, // Set the map display level to 10 projection: 'EPSG:4326' //Set projection}), // Let the div with id map be the container of the map target: 'map' }) </script> </body> </html> 4. Operation results 3. Get all features on the vector map and set the style1.map2.html <!Doctype html> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html;charset=utf-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'> <meta content='always' name='referrer'> <title>OpenLayers 3: Get all features on the vector map and set the style</title> <link href='ol.css ' rel='stylesheet' type='text/css'/> <script type='text/javascript' src='ol.js' charset='utf-8'></script> </head> <body> <div id='map' style='width: 800px;height:500px;margin: auto'></div> <br> <div style='width: 800px;margin: auto'> <button type="button" onclick = 'updateStyle()' >Modify Feature style</button> </div> <script> /** * Create a map */ var map = new ol.Map({ // Set the map layers layers: [ //Create a layer that uses the Open Street Map map source new ol.layer.Tile({ source: new ol.source.OSM() }), ], // Set the view to display the map view: new ol.View({ center: [104,30], // Set the map display center to longitude 104 degrees and latitude 30 degrees zoom: 10, // Set the map display level to 10 projection: 'EPSG:4326' //Set projection}), // Let the div with id map be the container of the map target: 'map' }); //Create a vector map source layer and set the style var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: 'geojson/map.geojson', // Map source format: new ol.format.GeoJSON() // Formatting class for parsing vector maps}), // Set the style, color is green, line thickness is 1 pixel style: new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'green', size: 1 }) }) }); map.addLayer(vectorLayer); /** * Get all the features on the vector layer and set the style */ function updateStyle(){ //Create a style with red color and 3 pixel line thickness var featureStyle = new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'red', size: 3 }) }) //Get all the features on the vector layer var features = vectorLayer.getSource().getFeatures() //Traverse all Features and set the style for each Feature for (var i = 0; i < features.length; i++) { features[i].setStyle(featureStyle) } } </script> </body> </html> 2. Operation results 4. Vector map coordinate system conversion Vector maps use 1.map3.html <!Doctype html> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html;charset=utf-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'> <meta content='always' name='referrer'> <title>OpenLayers 3: Vector map coordinate system conversion</title> <link href='ol.css ' rel='stylesheet' type='text/css'/> <script type='text/javascript' src='ol.js' charset='utf-8'></script> <script src="jquery-3.6.0.js"></script> </head> <body> <div id='map' style='width: 1000px;height: 800px;margin: auto'></div> <script> /** * Create a map */ var map = new ol.Map({ // Set the map layers layers: [ //Create a layer that uses the Open Street Map map source new ol.layer.Tile({ source: new ol.source.OSM() }) ], // Set the view to display the map view: new ol.View({ center: ol.proj.fromLonLat([104,30]), // Set the map display center at longitude 104 degrees and latitude 30 degrees zoom: 10, // Set the map display level to 10 }), // Let the div with id map be the container of the map target: 'map' }); // Load vector map function addGeoJSON(data) { var layer = new ol.layer.Vector({ source: new ol.source.Vector({ features: (new ol.format.GeoJSON()).readFeatures(data, { // Use the readFeatures method to customize the coordinate system dataProjection: 'EPSG:4326', // Set the coordinate system used by the JSON data featureProjection: 'EPSG:3857' // Set the coordinate system of the feature used by the current map }) }) }); map.addLayer(layer); }; $.ajax({ url: 'geojson/map.geojson', success: function(data, status) { // After successfully obtaining the data content, call the method to add the vector map to the map addGeoJSON(data); } }); </script> </body> </html> 2. Operation results This is the end of this article about OpenLayers 3 loading vector map source. For more information about OpenLayers 3 loading vector map, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Specific use of MySQL operators (and, or, in, not)
>>: Introduction to partitioning the root directory of Ubuntu with Vmvare virtual machine
Preface Not long ago, I combined browser-sync+gul...
Preface: Basically, whether it is for our own use...
Table of contents 1. Control the display and hidi...
The following case reviews the knowledge points o...
Install the nvidia graphics card driver under Ubu...
The specific code is as follows: package epoint.m...
Table of contents Inheritance ES5 prototype inher...
Install MySQL database a) Download the MySQL sour...
Docker supports running on the following CentOS v...
One day I found that the execution speed of a SQL...
On Saturday, the redis server on the production s...
1. Location regular expression Let's take a l...
When the scale of Docker deployment becomes large...
1. MySQL rpm package installation # Download the ...
Preface In front-end development, we often encoun...