Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <title>Vector layer example</title>
- <link rel="stylesheet" href="https://openlayers.org/en/v3.19.1/css/ol.css" type="text/css">
- <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
- <script src="https://openlayers.org/en/v3.19.1/build/ol.js"></script>
- <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
- <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
- <style>
- #popup-content {
- background-color: lightgrey;
- }
- .ol-popup {
- position: absolute;
- background-color: white;
- -webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
- filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
- padding: 15px;
- border-radius: 10px;
- border: 1px solid #cccccc;
- bottom: 12px;
- left: -50px;
- min-width: 280px;
- }
- .ol-popup:after, .ol-popup:before {
- top: 100%;
- border: solid transparent;
- content: " ";
- height: 0;
- width: 0;
- position: absolute;
- pointer-events: none;
- }
- .ol-popup:after {
- border-top-color: white;
- border-width: 10px;
- left: 48px;
- margin-left: -10px;
- }
- .ol-popup:before {
- border-top-color: #cccccc;
- border-width: 11px;
- left: 48px;
- margin-left: -11px;
- }
- .ol-popup-closer {
- text-decoration: none;
- position: absolute;
- top: 2px;
- right: 8px;
- }
- .ol-popup-closer:after {
- content: "✖";
- }
- </style>
- </head>
- <body>
- <div id="map" class="map">
- <div id="popup" class="ol-popup">
- <a href="#" id="popup-closer" class="ol-popup-closer"></a>
- <div id="popup-content" > </div>
- </div>
- </div>
- <div id="info" class="alert alert-success">
-
- </div>
- <script>
- $( function() {
- $( "#popup" ).draggable();
- $("#popup").resizable();
- } );
- var container = document.getElementById('popup');
- var content = document.getElementById('popup-content');
- var closer = document.getElementById('popup-closer');
- var style_g = new ol.style.Style({
- fill: new ol.style.Fill({
- color: 'rgba(255, 255, 255, 0.3)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0,255,255,0.6)',
- width: 1
- })
- });
- var style_k = new ol.style.Style({
- fill: new ol.style.Fill({
- color: 'rgba(255, 255, 255, 0.3)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0, 128, 0,0.6)',
- width: 1
- })
- });
- var style_l = new ol.style.Style({
- fill: new ol.style.Fill({
- color: 'rgba(255, 255, 255, 0.3)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0, 128, 128,0.6)',
- width: 1
- })
- });
- var style_m = new ol.style.Style({
- fill: new ol.style.Fill({
- color: 'rgba(255, 255, 255, 0.3)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0, 0, 255,0.6)',
- width: 1
- })
- });
- styles_g = [style_g,style_k,style_l,style_m];
- for (i=0;i<styles_g.length;i++)
- {
- var vectorLayer4 = new ol.layer.Vector({
- maxResolution: 0.8,
- source: new ol.source.Vector({
- url: 'pms_geom_php.php',
- format: new ol.format.GeoJSON({
- defaultDataProjection:'EPSG:4326'
- })
- }),
- style: styles_g[i]
- });
- //alert(styles_g[i]);
- }
- var popup = new ol.Overlay(({
- element: container,
- autoPan: true,
- autoPanAnimation: {
- duration: 250
- }
- }));
- closer.onclick = function() {
- popup.setPosition(undefined);
- closer.blur();
- return false;
- };
- var mapcenter = [79.60,23.44];
- var map = new ol.Map({
- layers: [
- new ol.layer.Tile({
- source: new ol.source.TileWMS({
- url: 'http://localhost:8089/geoserver/manjula/wms',
- params: {'LAYERS': 'manjula:India_states_UT_geo_wgs84'}
- })
- }),
- vectorLayer4
- ],
- overlays: [popup],
- target: 'map',
- view: new ol.View({
- projection: 'EPSG:4326',
- center: mapcenter,
- zoom: 5,
- extent: [66.2329, 7.68083, 98.2223, 39.03874],
- maxZoom:16,
- minZoom: 4,
- })
- });
- var highlightStyleCache = {};
- var featureOverlay = new ol.layer.Vector({
- source: new ol.source.Vector(),
- map: map,
- style: function (feature, resolution) {
- var text = resolution * 100000 < 10 ? feature.get('text') : '';
- if (!highlightStyleCache[text]) {
- highlightStyleCache[text] = new ol.style.Style({
- stroke: new ol.style.Stroke({
- color: '#0000FF',
- width: 2
- }),
- fill: new ol.style.Fill({
- color: 'rgba(192,192,192,0.7)'
- }),
- text: new ol.style.Text({
- font: '12px Calibri,sans-serif',
- text: text,
- fill: new ol.style.Fill({
- color: '#000'
- }),
- stroke: new ol.style.Stroke({
- color: '#f00',
- width: 3
- })
- })
- });
- }
- return highlightStyleCache[text];
- }
- });
- var highlight;
- var displayFeatureInfo = function (pixel) {
- var feature = map.forEachFeatureAtPixel(pixel, function (feature) {
- return feature;
- });
- var info = document.getElementById('info');
- if (feature) {
- info.innerHTML = '';
- } else {
- info.innerHTML = ' ';
- }
- if (feature !== highlight) {
- if (highlight) {
- featureOverlay.getSource().removeFeature(highlight);
- }
- if (feature) {
- featureOverlay.getSource().addFeature(feature);
- }
- highlight = feature;
- }
- };
- map.on('singleclick', function(evt) {
- if (evt.dragging) {
- return;
- }
- var pixel = map.getEventPixel(evt.originalEvent);
- displayFeatureInfo(pixel);
- feature = evt.coordinate;
- var hdms = ol.coordinate.toStringHDMS(feature);
- click_long = feature[1].toFixed(2);
- click_lat = feature[0].toFixed(2);
- alert("lat"+click_lat);
- alert("lat"+click_lat);
- var xmlhttp;
- if( window.XMLHttpRequest )
- {
- xmlhttp = new XMLHttpRequest();
- }
- else
- {
- xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
- }
- xmlhttp.onreadystatechange = function( )
- {
- if( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
- {
- content.innerHTML = '<p style="color:orange">You clicked here:</p>'+ xmlhttp.responseText;
- }
- }
- var url = "pms_spatial_inc.php";
- url=url+"?hdms="+hdms;
- url=url+"&click_lat="+click_lat;
- url=url+"&click_long="+click_long;
- xmlhttp.open( "GET", url, true );
- xmlhttp.send();
- popup.setPosition(feature);
- });
- map.addOverlay( popup);
- map.on('click', function (evt) {
- var pixel = map.getEventPixel(evt.originalEvent);
- displayFeatureInfo(evt.pixel);
- })
- </script>
- </body>
- </html>
- <?php
- $host = "host=192.168.106.81";
- $port = "port=5432";
- $dbname = "dbname=pms";
- $credentials = "user=postgres password=postgres";
- $db = pg_connect("$host $port $dbname $credentials");
- if(!$db){
- echo "Error : Unable to open databasen";
- }
- $data_query = "SELECT ST_AsGeoJSON(geom) As geometry from three";
- $data = pg_query($db,$data_query);
- $geojson = array(
- 'type' => 'FeatureCollection',
- 'features' => array()
- );
- while($edge=pg_fetch_assoc($data)) {
- $feature = array(
- 'type' => 'Feature',
- 'geometry' => $edge['geometry'],
- );
- array_push($geojson['features'], $feature);
- }
- echo str_replace('}"','}',str_replace('"{"','{"',stripslashes(json_encode($geojson))));
- ?>
- var style_g = new ol.style.Style({
- fill: new ol.style.Fill({
- color: 'rgba(255, 255, 255, 0.3)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0,255,255,0.6)',
- width: 1
- })
- });
- var style_k = new ol.style.Style({
- fill: new ol.style.Fill({
- color: 'rgba(255, 255, 255, 0.3)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0, 128, 0,0.6)',
- width: 1
- })
- });
- var style_l = new ol.style.Style({
- fill: new ol.style.Fill({
- color: 'rgba(255, 255, 255, 0.3)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0, 128, 128,0.6)',
- width: 1
- })
- });
- var style_m = new ol.style.Style({
- fill: new ol.style.Fill({
- color: 'rgba(255, 255, 255, 0.3)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0, 0, 255,0.6)',
- width: 1
- })
- });
- styles_g = [style_g,style_k,style_l,style_m];
- for (i=0;i<styles_g.length;i++)
- {
- var vectorLayer4 = new ol.layer.Vector({
- maxResolution: 0.8,
- source: new ol.source.Vector({
- url: 'pms_geom_php.php',
- format: new ol.format.GeoJSON({
- defaultDataProjection:'EPSG:4326'
- })
- }),
- style: styles_g[i]
- });
- //alert(styles_g[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement