如何在React-Native中为MapView组件计算纬度和经度? [英] How to calculate delta latitude and longitude for MapView component in React-Native?

查看:72
本文介绍了如何在React-Native中为MapView组件计算纬度和经度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何计算React-Native中MapView组件的纬度和经度值形成的纬度和经度值.谢谢

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

推荐答案

如果您有一个坐标数组,并且想要一个适合所有这些点的区域,则可以执行以下操作:

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

然后将region与react-native-maps配合使用.

Then use region with react-native-maps.

注意:在我的示例中,我没有进行错误检查.

Note: I do no error checking in my example.

这篇关于如何在React-Native中为MapView组件计算纬度和经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆