javascript – Openlayers在OpenStreetMaps图层上圈出多边形

前端之家收集整理的这篇文章主要介绍了javascript – Openlayers在OpenStreetMaps图层上圈出多边形前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个带有已定义中心的圆圈,并在其上放置一个图标标记.如果我使用图像而不是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.当你创建圆圈时,新的OpenLayers.Geometry.Point(lonLat)应该是新的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',graphicXOffset: -12,graphicYOffset: -25 }
);
vectorLayer.addFeatures([featurePoint,featurecircle]);

map.addLayer(vectorLayer);
原文链接:https://www.f2er.com/js/155741.html

猜你在找的JavaScript相关文章