H
H
HorNik2016-03-22 10:16:57
JSON
HorNik, 2016-03-22 10:16:57

How to set information about objects on the map using Openlayers?

Recently, I just plunged into this environment and it’s quite difficult to figure it out on my own. My task is to deal with openlayers. First you need to create a small program that displays a small street and when you click on the building, display some information.
Everything I've done so far is shown below.

<!DOCTYPE html>
<html>
  <head>
    <title>GeoJSON</title>
    <link rel="stylesheet" href="ol.css" type="text/css">
    <script src="ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script>
      var image = new ol.style.Circle({
        radius: 5,
        fill: null,
        stroke: new ol.style.Stroke({color: 'red', width: 1})
      });

      var styles = {
        'Point': new ol.style.Style({
          image: image
        }),
        'LineString': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: '#ffffff',
            width: 1
          })
        }),
    
    //Стиль линий
        'MultiLineString': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: '#000000',
            width: 1
          })
        }),
        
    
    //Стиль полигона
        'MultiPolygon': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: '#000000',
            width: 1
          }),
          fill: new ol.style.Fill({
            color: 'rgba(255, 255, 0, 0.9)'
          })
        }),
        
       
        'Circle': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: 'red',
            width: 2
          }),
          fill: new ol.style.Fill({
            color: 'rgba(255,0,0,0.2)'
          })
        })
      };

      var styleFunction = function(feature) {
        return styles[feature.getGeometry().getType()];
      };

    // Описание системы координат и объектов
      var geojsonObject = {
    //Описание системы координат
        'type': 'FeatureCollection',
        'crs': {
          'type': 'name',
          'properties': {
            'name': 'EPSG:3857'
          }
        },
        'features': [ {
          'type': 'Feature',
          'geometry': {
            'type': 'MultiLineString',
            'coordinates': [
              ,
        ,
        
        ,,,,,,,
            ]
          }
        }, {
          'type': 'Feature',
          'geometry': {
            'type': 'MultiPolygon',
            'coordinates': [
              
        [],
        [],
        [],
        [],
        
        
        
        
              [],
        [],
        [],
        
            ]
          }
        }]
      };
  
    
    //Обявление подключения библиотеки GeoJSON
      var vectorSource = new ol.source.Vector({
        features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
      });
  
    
    //Сбор этого всего в 1 слой.
      var vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        style: styleFunction
      });
    
    //Создание обекта - "Карта"
      var map = new ol.Map({
        layers: [
          vectorLayer
        ],
        target: 'map',
    //Добавнение пользовательского интерфейса
        controls: ol.control.defaults({
          attributionOptions:  ({
            collapsible: false
          })
        }),
    
    //Позиция камеры при загрузке
        view: new ol.View({
          center: [-4e5, 2e5],
          zoom: 6
        })
      });
    </script>
  </body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pan Propan, 2016-03-22
@mgis

Ask this question here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question