Friday, 20 July 2018

Is it possible to calculate region based on specified coordinate and screen size

I have a coordinate and I want to set map region for it.
Problem is that when I pass coordinate to create a region for it to a function that coordinate is in the center of region.
I don't want it to be in center I need it up a bit.
If for example map is 500 in height I want to set region so that coordinate is in the center of top 250.

function getRegionForCoordinates(points) {
  // points should be an array of { latitude: X, longitude: Y }
  let minX, maxX, minY, maxY;

  // init first point
  ((point) => {
    minX = point.latitude;
    maxX = point.latitude;
    minY = point.longitude;
    maxY = point.longitude;
  })(points[0]);

  // calculate rect
  points.map((point) => {
    minX = Math.min(minX, point.latitude);
    maxX = Math.max(maxX, point.latitude);
    minY = Math.min(minY, point.longitude);
    maxY = Math.max(maxY, point.longitude);
  });

  const midX = (minX + maxX) / 2;
  const midY = (minY + maxY) / 2;
  let deltaX = (maxX - minX);
  let deltaY = (maxY - minY);

  return {
    latitude: midX,
    longitude: midY,
    latitudeDelta: 0.002,
    longitudeDelta: 0.002
  };
}

Is this possible to calculate?
enter image description here



from Is it possible to calculate region based on specified coordinate and screen size

No comments:

Post a Comment