簡體   English   中英

Openlayers在OpenStreetMaps圖層上圈出Polygon

[英]Openlayers circle Polygon on OpenStreetMaps layer

我正在嘗試創建一個帶有已定義中心的圓圈,並在其上放置一個圖標標記。 如果我使用圖像而不是OpenLayers.Geometry.Polygon.createRegularPolygon,代碼正常工作。 我無法解決它。

在這里你找到我的代碼:

<html>
<head>
<title>OpenLayers Example</title>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
</head>
<body>

<div id="mapdiv"></div>
<script>

map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM());

epsg4326 =  new OpenLayers.Projection("EPSG:4326"); //WGS 1984 projection
projectTo = map.getProjectionObject(); //The map projection (Spherical Mercator)

var lonLat = new OpenLayers.LonLat( -0.1279688 ,51.5077286 ).transform(epsg4326,     projectTo);

var zoom=6;
map.setCenter (lonLat, zoom);

var mycircle = OpenLayers.Geometry.Polygon.createRegularPolygon(
               new OpenLayers.Geometry.Point( lonLat ),
               1,
               30
           );

var featurecircle = new OpenLayers.Feature.Vector(mycircle);


var vectorLayer = new OpenLayers.Layer.Vector("Overlay");

// Define markers as "features" of the vector layer:
vectorLayer.addFeatures(featurecircle);

var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point( -0.1244324, 51.5006728  ).transform(epsg4326, projectTo),
        {description:'info'} ,
        {externalGraphic: 'img/marker.png', graphicHeight: 25, graphicWidth: 21,      graphicXOffset:-12, graphicYOffset:-25  }
    );    
vectorLayer.addFeatures(feature);


map.addLayer(vectorLayer);


</script>
</body>
</html>

提前感謝任何提示。

OpenLayers.Geometry.Point構造函數接受x,y而不是lonlat obj。 當你創建圓圈時, new OpenLayers.Geometry.Point( lonLat )應該是new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);

在此輸入圖像描述

這應該更好:

map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM());
epsg4326 = new OpenLayers.Projection("EPSG:4326"); //WGS 1984 projection
projectTo = map.getProjectionObject(); //The map projection (Spherical Mercator)

var lonLat = new OpenLayers.LonLat(-0.1279688, 51.5077286).transform(epsg4326, projectTo);

var zoom = 6;
map.setCenter(lonLat, zoom);

var vectorLayer = new OpenLayers.Layer.Vector("Overlay");

var point = new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);


var mycircle = OpenLayers.Geometry.Polygon.createRegularPolygon
(
    point,
    50000,
    40,
    0
);

var featurecircle = new OpenLayers.Feature.Vector(mycircle);

var featurePoint = new OpenLayers.Feature.Vector(
    point,
    { description: 'info' },
    { externalGraphic: 'img/marker.png', graphicHeight: 25, graphicWidth: 21, graphicXOffset: -12, graphicYOffset: -25 }
);
vectorLayer.addFeatures([featurePoint, featurecircle]);

map.addLayer(vectorLayer);

如果你想將你的圓和你的點組合成一個對象,那么使用OpenLayers.Geometry.Collection 使用此方法,您可以應用一些控件,如DragFeature,它將立即對集合中的元素進行操作。

var defaultStyle = new OpenLayers.Style({
    externalGraphic:'${icon}',
    graphicHeight: 25, 
    graphicWidth:  21,      
    graphicXOffset:-12, 
    graphicYOffset:-25
});
var styleMap = new OpenLayers.StyleMap({'default': defaultStyle });
var vectorLayer = new OpenLayers.Layer.Vector("Overlay",{styleMap: styleMap });

var aPoint  = new OpenLayers.Geometry.Point( lonLat.lon, lonLat.lat );
var aCircle = OpenLayers.Geometry.Polygon.createRegularPolygon( aPoint, 50000, 40, 0 );

var aCirclePoint = new OpenLayers.Geometry.Collection( [ aCircle, aPoint ] );
var aCirclePoint_feature = new OpenLayers.Feature.Vector( aCirclePoint );
aCirclePoint_feature.attributes = { icon:'/img/marker.png' }

vectorLayer.addFeatures( [ aCirclePoint_feature ] );    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM