How to calculate delta latitude and longitude for MapView component in React-Native?

谁都会走 提交于 2019-12-21 09:24:08

问题


How can i calculate delta-latitude and delta-longitude values form latitude and longitude values for MapView component in React-Native. Thank You


回答1:


If you have an array of coordinates and you want a region that will fit all these points, you can do something like this:

const regionContainingPoints = points => {
  let minLat, maxLat, minLng, maxLng;

  // init first point
  (point => {
    minLat = point.latitude;
    maxLat = point.latitude;
    minLng = point.longitude;
    maxLng = point.longitude;
  })(points[0]);

  // calculate rect
  points.forEach(point => {
    minLat = Math.min(minLat, point.latitude);
    maxLat = Math.max(maxLat, point.latitude);
    minLng = Math.min(minLng, point.longitude);
    maxLng = Math.max(maxLng, point.longitude);
  });

  const midLat = (minLat + maxLat) / 2;
  const midLng = (minLng + maxLng) / 2;

  const deltaLat = (maxLat - minLat);
  const deltaLng = (maxLng - minLng);

  return {
    latitude: midLat, longitude: midLng,
    latitudeDelta: deltaLat, longitudeDelta: deltaLng,
  };
}

// example region
const region = regionContainingPoints([
  {latitude: 47.12345, longitude: -124.12345},
  {latitude: 47.23456, longitude: -124.23456},
  {latitude: 47.34567, longitude: -124.34567},
]);

Then use region with react-native-maps.

Note: I do no error checking in my example.




回答2:


longitudeDelta and latitudeDelta is described will in the source code MapView, where it says difference between the minimum and maximum points that you want displayed.

So, if you want to find the latitudeDelta then you should calculate the max difference between your latitudes and likewise for the longitude delta. You can look at Ryan H.'s answer to see an actual implementation.



来源:https://stackoverflow.com/questions/32557474/how-to-calculate-delta-latitude-and-longitude-for-mapview-component-in-react-nat

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