gpt4 book ai didi

javascript - 使用 Open Layers 自动绘制路径的第一个顶点

转载 作者:行者123 更新时间:2023-11-30 17:13:50 29 4
gpt4 key购买 nike

我想帮助用户使用 OpenLayers 输入路段的方向。

我有那种用户可以输入方位的表格,但我想通过以下方式帮助他:

  • 当用户点击按钮时,开始在 map 上绘制线段的第一个顶点(第一个顶点是已知点)
  • 然后用户只需点击第二个顶点,方位 Angular 就会自动计算。

参见 fiddle here或下面的 SO 片段。

我快完成了:我可以在绘制线段时计算方位。但是在脚本的最后有一个异常(exception):我无法让 OL 自动绘制我的线段的第一个点。

感谢任何能提供帮助的人。

<script src="http://openlayers.org/api/OpenLayers.js"></script>

<body>
<div id="map" style="height: 500px"></div>
</body>



<script>
var CONSTANTS = {
MAP_FROM_PROJECTION: new OpenLayers.Projection("EPSG:4326"), // Transform from WGS 1984
MAP_TO_PROJECTION: new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
};

function radians(n) {
return n * (Math.PI / 180);
}

function degrees(n) {
return n * (180 / Math.PI);
}


function computeBearing(startLat, startLong, endLat, endLong) {
startLat = radians(startLat);
startLong = radians(startLong);
endLat = radians(endLat);
endLong = radians(endLong);

var dLong = endLong - startLong;

var dPhi = Math.log(Math.tan(endLat / 2.0 + Math.PI / 4.0) / Math.tan(startLat / 2.0 + Math.PI / 4.0));
if (Math.abs(dLong) > Math.PI) {
if (dLong > 0.0) dLong = -(2.0 * Math.PI - dLong);
else dLong = (2.0 * Math.PI + dLong);
}

return (degrees(Math.atan2(dLong, dPhi)) + 360.0) % 360.0;
}


map = new OpenLayers.Map("map");
map.addLayer(new OpenLayers.Layer.OSM());
map.setCenter(new OpenLayers.LonLat(3, 47).transform(CONSTANTS.MAP_FROM_PROJECTION, CONSTANTS.MAP_TO_PROJECTION), 6);


var lineLayer = new OpenLayers.Layer.Vector("Line Layer");

map.addLayers([lineLayer]);

var lineControl = new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path, {
handlerOptions: {
maxVertices: 2,
freehandMode: function(evt) {
return false;
}
},
featureAdded: function(feature) {
var drawnLinePoints = feature.geometry.getVertices();
var lonlat1 = drawnLinePoints[0].transform(CONSTANTS.MAP_TO_PROJECTION, CONSTANTS.MAP_FROM_PROJECTION);
var lonlat2 = drawnLinePoints[1].transform(CONSTANTS.MAP_TO_PROJECTION, CONSTANTS.MAP_FROM_PROJECTION);
var bearingValue = computeBearing(lonlat1.y, lonlat1.x, lonlat2.y, lonlat2.x);
console.log(bearingValue);
}
});
map.addControl(lineControl);
lineControl.activate();


var handler;
for (var i = 0; i < map.controls.length; i++) {
var control = map.controls[i];
if (control.displayClass === "olControlDrawFeature") {
handler = control.handler;
break;
}
}

// Here I have an exception in the console : I would like
// OL to draw hat point automatically.
handler.addPoint(new OpenLayers.Pixel(50, 50));
</script>

最佳答案

OpenLayers.Handler.Path.addPoint 适用于 OpenLayers.Pixel,而不是 OpenLayers.LonLat:

/**
* Method: addPoint
* Add point to geometry. Send the point index to override
* the behavior of LinearRing that disregards adding duplicate points.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The pixel location for the new point.
*/
addPoint: function(pixel) {
this.layer.removeFeatures([this.point]);
var lonlat = this.layer.getLonLatFromViewPortPx(pixel);
this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
);
this.line.geometry.addComponent(
this.point.geometry, this.line.geometry.components.length
);
this.layer.addFeatures([this.point]);
this.callback("point", [this.point.geometry, this.getGeometry()]);
this.callback("modify", [this.point.geometry, this.getSketch()]);
this.drawFeature();
delete this.redoStack;
}

除了添加 addPointByLonLat 方法之外,我实际上没有看到实现此目的的好方法:

OpenLayers.Handler.Path.prototype.addPointByLonLat = function(lonLat) {
this.layer.removeFeatures([this.point]);
this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
);
this.line.geometry.addComponent(
this.point.geometry, this.line.geometry.components.length
);
this.layer.addFeatures([this.point]);
this.callback("point", [this.point.geometry, this.getGeometry()]);
this.callback("modify", [this.point.geometry, this.getSketch()]);
this.drawFeature();
delete this.redoStack;
};

或者子类化为您自己的处理程序类(可能更干净)。

注意事项:

  • addPoint 不是 API 方法(所以 addPointByLonLat 也不是)。这可能会导致版本更改出现问题。
  • 不要在开发中使用压缩/缩小的 JS,并检查您使用的方法的文档。
  • 下次考虑在 https://gis.stackexchange.com/ 上询问.
  • 考虑要求对您的 JS 进行代码审查。

关于javascript - 使用 Open Layers 自动绘制路径的第一个顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26484372/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com