OpenLayers compute offset coordinates

让人想犯罪 __ 提交于 2019-12-12 00:02:56

问题


I need a way of drawing polygons with openLayers based on lat/long coordinates, rotation and lenght (in meters).

Example: "I want to draw a line from point 1 (lat,long) to point 2 where point 2 is calculated based on the fact that it is located 115 meters with the rotation of 115 degrees from point 1"

Google maps has an easy way of calculating coordinates by using spherical.computeOffset() method. Does OpenLayers has anything similar? Or are there suggestions of other nice open source libraries to include that can help me?


回答1:


Have a look at https://github.com/openlayers/ol3/blob/master/src/ol/sphere/sphere.js#L256

It's not api, but should be easy to copy and modify to your code.

/**
 * Returns the coordinate at the given distance and bearing from `c1`.
 *
 * @param {ol.Coordinate} c1 The origin point (`[lon, lat]` in degrees).
 * @param {number} distance The great-circle distance between the origin
 *     point and the target point.
 * @param {number} bearing The bearing (in radians).
 * @return {ol.Coordinate} The target point.
 */
ol.Sphere.prototype.offset = function(c1, distance, bearing) {
  var lat1 = goog.math.toRadians(c1[1]);
  var lon1 = goog.math.toRadians(c1[0]);
  var dByR = distance / this.radius;
  var lat = Math.asin(
      Math.sin(lat1) * Math.cos(dByR) +
      Math.cos(lat1) * Math.sin(dByR) * Math.cos(bearing));
  var lon = lon1 + Math.atan2(
      Math.sin(bearing) * Math.sin(dByR) * Math.cos(lat1),
      Math.cos(dByR) - Math.sin(lat1) * Math.sin(lat));
  return [goog.math.toDegrees(lon), goog.math.toDegrees(lat)];
};


来源:https://stackoverflow.com/questions/31680745/openlayers-compute-offset-coordinates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!