ol.html 6.67 KB

<html xmlns="http://www.w3.org/1999/xhtml"> 
  <head> 
    <title>OpenLayers: Vector Features</title> 
    <link rel="stylesheet" href="../theme/default/style.css" type="text/css" /> 
    <link rel="stylesheet" href="style.css" type="text/css" /> 
    <script src="../OpenLayers-2.11/lib/OpenLayers.js" type="text/javascript"></script> 

    <script type="text/javascript" src="../src/jsts.js"></script>
	<script type="text/javascript" src="../src/jsts/geom.js"></script>
	<script type="text/javascript" src="../src/jsts/geom/Geometry.js"></script>
	<script type="text/javascript" src="../src/jsts/geom/Coordinate.js"></script>
	<script type="text/javascript" src="../src/jsts/geom/Envelope.js"></script>
	<script type="text/javascript" src="../src/jsts/geom/GeometryFactory.js"></script>
	<script type="text/javascript" src="../src/jsts/geom/LineString.js"></script>
	<script type="text/javascript" src="../src/jsts/geom/LinearRing.js"></script>
	<script type="text/javascript" src="../src/jsts/geom/Point.js"></script>
	<script type="text/javascript" src="../src/jsts/geom/Polygon.js"></script>
	
	<script type="text/javascript" src="../src/jsts/geom/mixin.js"></script>
    
    <script type="text/javascript"> 
        var map;
 
        function init(){
            map = new OpenLayers.Map('map');
            var layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                    "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
            map.addLayer(layer);
 
            /*
             * Layer style
             */
            // we want opaque external graphics and non-opaque internal graphics
            var layer_style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
            layer_style.fillOpacity = 0.2;
            layer_style.graphicOpacity = 1;
 
            /*
             * Blue style
             */
            var style_blue = OpenLayers.Util.extend({}, layer_style);
            style_blue.strokeColor = "blue";
            style_blue.fillColor = "blue";
            style_blue.graphicName = "star";
            style_blue.pointRadius = 10;
            style_blue.strokeWidth = 3;
            style_blue.rotation = 45;
            style_blue.strokeLinecap = "butt";
 
            /*
             * Green style
             */
            var style_green = {
                strokeColor: "#00FF00",
                strokeWidth: 3,
                strokeDashstyle: "dashdot",
                pointRadius: 6,
                pointerEvents: "visiblePainted"
            };
 
            /*
             * Mark style
             */
            var style_mark = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
            // each of the three lines below means the same, if only one of
            // them is active: the image will have a size of 24px, and the
            // aspect ratio will be kept
            // style_mark.pointRadius = 12;
            // style_mark.graphicHeight = 24; 
            // style_mark.graphicWidth = 24;
 
            // if graphicWidth and graphicHeight are both set, the aspect ratio
            // of the image will be ignored
            style_mark.graphicWidth = 24;
            style_mark.graphicHeight = 20;
            style_mark.graphicXOffset = 10; // default is -(style_mark.graphicWidth/2);
            style_mark.graphicYOffset = -style_mark.graphicHeight;
            style_mark.externalGraphic = "../img/marker.png";
            // graphicTitle only works in Firefox and Internet Explorer
			style_mark.graphicTitle = "this is a test tooltip";
 
            var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {style: layer_style});
 
            // create a point feature
            var point = new jsts.geom.Coordinate(-111.04, 45.68);
            var pointFeature = new OpenLayers.Feature.Vector(point,null,style_blue);
            var point2 = new jsts.geom.Coordinate(-105.04, 49.68);
            var pointFeature2 = new OpenLayers.Feature.Vector(point2,null,style_green);
            var point3 = new jsts.geom.Coordinate(-105.04, 49.68);
            var pointFeature3 = new OpenLayers.Feature.Vector(point3,null,style_mark);
 
            
            // create a line feature from a list of points
            var pointList = [];
            var newPoint = point;
            for(var p=0; p<15; ++p) {
                newPoint = new jsts.geom.Coordinate(newPoint.x + Math.random(1),
                                                         newPoint.y + Math.random(1));
                pointList.push(newPoint);
            }
            var lineFeature = new OpenLayers.Feature.Vector(
                new jsts.geom.LineString(pointList),null,style_green);
 
            
            // create a polygon feature from a linear ring of points
            var pointList = [];
            for(var p=0; p<6; ++p) {
                var a = p * (2 * Math.PI) / 7;
                var r = Math.random(1) + 1;
                var newPoint = new jsts.geom.Coordinate(point.x + (r * Math.cos(a)),
                                                             point.y + (r * Math.sin(a)));
                pointList.push(newPoint);
            }
            pointList.push(pointList[0]);
 
            var linearRing = new jsts.geom.LinearRing(pointList);
            var polygon = new jsts.geom.Polygon([linearRing]);
            var polygonFeature = new OpenLayers.Feature.Vector(polygon);
            
            map.addLayer(vectorLayer);
            map.setCenter(new OpenLayers.LonLat(point.x, point.y), 5);
            vectorLayer.addFeatures([pointFeature, pointFeature3, pointFeature2, lineFeature, polygonFeature]);
        }
    </script> 
  </head> 
  <body onload="init()"> 
<h1 id="title">Drawing Simple Vector Features Example</h1> 
 
<div id="tags"> 
</div> 
<p id="shortdesc"> 
    Shows the use of the shows drawing simple vector features, in different styles.
</p> 
<div id="map" class="smallmap"></div> 
<div id="docs"> 
    <p>This example shows drawing simple vector features -- point, line, polygon
       in different styles, created 'manually', by constructing the entire style
       object, via 'copy', extending the default style object, and by
       inheriting the default style from the layer.</p> 
    <p>It also shows how to use external graphic files for point features
       and how to set their size: If either graphicWidth or graphicHeight is set,
       the aspect ratio of the image will be respected. If both graphicWidth and
       graphicHeight are set, it will be ignored. Alternatively, if graphicWidth
       and graphicHeight are omitted, pointRadius will be used to set the size
       of the image, which will then be twice the value of pointRadius with the
       original aspect ratio.</p> 
</div> 
 
  </body> 
</html>