简体   繁体   English

Openlayers:GeoJSON 和坐标

[英]Openlayers: GeoJSON and Coordinates

I'm trying to parse GeoJSON data and draw 'Points' on a ESPG:4326 map, with some success.我正在尝试解析 GeoJSON 数据并在 ESPG:4326 地图上绘制“点”,并取得了一些成功。 It draws the 'Point' on the map, but on the default setting [0, 0].它在地图上绘制“点”,但使用默认设置 [0, 0]。 I am new to Openlayers.我是 Openlayers 的新手。 I have also put my geojson through two validators, which came back with no errors.我还将我的 geojson 通过两个验证器,它们没有错误地返回。 My code below is a .js file.我下面的代码是一个 .js 文件。

I had a similar problem before when changing the 'center' of the map, where I used 'fromLonLat' to fix the issue.在更改地图的“中心”之前,我遇到了类似的问题,我使用“fromLonLat”来解决该问题。 I assume there is some sort of solution involving 'fromLonLat', but I cannot find anything related to this anywhere on Openlayers 6.我假设有某种涉及“fromLonLat”的解决方案,但我在 Openlayers 6 上的任何地方都找不到与此相关的任何内容。

import Feature from 'ol/Feature';
import Map from 'ol/Map';
import View from 'ol/View';
import GeoJSON from 'ol/format/GeoJSON';
import Point from 'ol/geom/Point';
import {fromLonLat} from 'ol/proj';
import {defaults as defaultInteractions, DragRotateAndZoom} from 'ol/interactio$
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {OSM, Vector as VectorSource} from 'ol/source';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';

var image = new CircleStyle({
  fill: new Fill({
        color: 'rgba(255, 0, 0, 1)'
        }),
  radius: 5,
  stroke: new Stroke({color: 'red', width: 1})
});


var markerStyle = {
  'Point': new Style({
    image: image
  })
};


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

var geojsonObject = {
   'type': 'FeatureCollection',
   'crs': {
       'type': 'name',
       'properties': {
        'name': 'ESPG:4326'
        }
  },
   'features': [
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.65, 54.34]
    },
    'properties': {
    'Site':'ARMAGH'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.86, 54.6]
    },
   'Site':'BELFAST EAST'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.97, 54.56]
    },
    'properties': {
    'Site':'BELFAST BALMORAL'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.27, 54.86]
    },
    'properties': {
    'Site':'BALLYMENA TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.26, 54.35]
    },
    'properties': {
    'Site':'BANBRIDGE TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.67, 54.65]
    },
    'properties': {
    'Site':'BANGOR REPEATER STATION'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.38, 55.09]
    },
    'properties': {
    'Site':'CREGAGH'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point','properties': {
    'Site':'BANBRIDGE TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.67, 54.65]
    },
    'properties': {
    'Site':'BANGOR REPEATER STATION'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.38, 55.09]
    },
    'properties': {
    'Site':'CREGAGH'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.66, 55.13]
    },
    'properties': {
    'Site':'COLERAINE REPEATER STATION'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.76, 54.50]
    },
    'properties': {
    'Site':'DUNGANNON'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.70, 54.32]
    },
    'properties': {
    'Site':'DOWNPATRICK TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-7.64, 54.34]
    },
    'properties': {
    'Site':'ENNISKILLEN'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.95, 54.66]
    },
    'properties': {
    'Site':'GLENGORMLEY'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.83374, 54.59279]
    },
    'properties': {
    'Site':'BELFAST KNOCK'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-7.30, 54.99]
    },
    'properties': {
    'Site':'LONDONDERRY MEG'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.82, 54.85]
    },
    'properties': {
    'Site':'LARNE TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.95, 55.05]
    },
    'properties': {
    'Site':'LIMAVADY'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.05, 54.51]
    },
    'properties': {
    'Site':'LISBURN'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.34, 54.45]
    },
    'properties': {
    'Site':'LURGAN'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.96, 54.55]
    },
    'properties': {
    'Site':'MALONE TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.60, 54.75]
    },
    'properties': {
    'Site':'MAGHERAFELT TRANSMISSION AREA'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.70, 54.59]
    },
    'properties': {
    'Site':'NEWTOWNARDS'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.33, 54.17]
    },
    'properties': {
    'Site':'NEWRY TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-7.29, 54.60]
    },
    'properties': {
    'Site':'OMAGH SPERRIN TRUNK'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.91, 54.57]
    },
    'properties': {
    'Site':'ORMEAU REPEATER STATION'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.88, 54.61]
    },
    'properties': {
    'Site':'INTAKE'
   }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.83, 54.45]
    },
    'properties': {
    'Site':'SAINTFIELD'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.89, 54.67]
    },
    'properties': {
    'Site':'WHITEABBEY'
    }
  }
]
}

var vectorSource = new VectorSource({
        features: new GeoJSON().readFeatures(geojsonObject, {
                dataProjection: 'ESPG:4326',
                featureProjection: 'ESPG:3857'
        })
});

var vectorLayer = new VectorLayer({
        source: vectorSource,
        style: styleFunction
});


var map = new Map({
  interactions: defaultInteractions().extend([
    new DragRotateAndZoom()
  ]),
  layers: [
    new TileLayer({
      source: new OSM()
    }),
    vectorLayer
  ],
  target: 'map',
  view: new View({
    center: fromLonLat([1.155670, 52.059311]),
    zoom: 2
  })
});



您的数据是 ESPG:4326,地图视图是 ESPG:3857,因此您需要功能:(new GeoJSON()).readFeatures(geojsonObject, {dataProjection: 'ESPG:4326', featureProjection: 'ESPG:3857'}) –迈克 23 小时前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM