OpenLayers compute offset coordinates

2019-08-21 16:39发布

问题:

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)];
};