From d135255a7523a2da3f22e73bfb32375b63e07852 Mon Sep 17 00:00:00 2001 From: Gooong Date: Sun, 8 Jul 2018 19:53:04 +0800 Subject: [PATCH] add geometry selection functionality --- .../static/jsx/map_view/map_view_map.jsx | 65 ++++++++++++------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/web/pgadmin/static/jsx/map_view/map_view_map.jsx b/web/pgadmin/static/jsx/map_view/map_view_map.jsx index 0f4b206f0d4..9dfe67873ca 100644 --- a/web/pgadmin/static/jsx/map_view/map_view_map.jsx +++ b/web/pgadmin/static/jsx/map_view/map_view_map.jsx @@ -12,7 +12,7 @@ import PropTypes from 'prop-types'; import ReactResizeDetector from 'react-resize-detector'; //require('ol/ol.css'); -var ol = require('ol/dist/ol'); +var ol = require('ol/dist/ol.js'); // import Map from 'ol/Map.js'; // import View from 'ol/View.js'; @@ -28,18 +28,26 @@ var ol = require('ol/dist/ol'); export default class MapViewMap extends React.Component{ constructor(props) { super(props); + this.initMap(); + this.onResize = this.onResize.bind(this); + } + initMap(){ this.backgroundLayer = new ol.layer.Tile({ source: new ol.source.OSM(), }); - this.dataLayer = new ol.layer.Vector(); + this.dataLayer = new ol.layer.Vector({ + renderMode: 'image', + }); this.mapView = new ol.View(); + let selectClick = new ol.interaction.Select({ + condition: ol.events.condition.click, + }); this.olMap = new ol.Map({ layers:[this.backgroundLayer, this.dataLayer], view: this.mapView, }); - - this.onResize = this.onResize.bind(this); + this.olMap.addInteraction(selectClick); } componentDidMount(){ @@ -47,32 +55,41 @@ export default class MapViewMap extends React.Component{ } componentDidUpdate(){ - //alert(JSON.stringify(this.props.geometries[0])); - - //render new data in map - const vectorSource = new ol.source.Vector({ - forrmat:new ol.format.GeoJSON(), - }); + let vectorSource = new ol.source.Vector(); let format = new ol.format.GeoJSON(); - let features = _.map(this.props.geometries, function (geometry) { - let geom = format.readFeature(geometry); - return geom; + let projection = new ol.proj.Projection({ + code: 'EPSG:' + this.props.SRID, + units: 'm', }); - vectorSource.addFeatures(features); - this.dataLayer.setSource(vectorSource); - this.renderSRID = this.props.SRID===0? 4326:this.props.SRID; - this.mapView = new ol.View({ - projection:'EPSG:' + this.renderSRID, - center:[0,0], - zoom:2, + let features = _.map(this.props.geometries, function (geometry) { + return format.readFeature(geometry, { + dataProjection: projection, + featureProjection: projection, + }); }); + vectorSource.addFeatures(features); - this.olMap.setProperties({ - layers: [this.dataLayer, this.backgroundLayer], - view: this.mapView, - }); + if (features.length > 0){ + this.dataLayer.setSource(vectorSource); + + alert(this.props.SRID); + alert(features.length); + alert(vectorSource.getExtent()); + if(this.renderSRID !== this.props.SRID){ + this.renderSRID = this.props.SRID; + this.mapView = new ol.View({ + projection: projection, + center:[0,0], + }); + } + this.olMap.setProperties({ + layers: [this.dataLayer, this.backgroundLayer ], + view: this.mapView, + }); + this.mapView.fit(vectorSource.getExtent(),{duration:500, constrainResolution: false}); + } }