Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate the dimensions of the current region in react-native-maps?

latitude: 38.69444432847513
latitudeDelta: 0.1288559458782288
longitude: -90.5969550254043
longitudeDelta: 0.1638044205986091

Suppose my map region has the data above (I use onRegionChange)

How can I get the distance (diameter) of the current region, in kilometers? (the width will be smaller than the height, because the phone is in portrait mode)

Is there an easy conversion I can do?

like image 384
TIMEX Avatar asked Dec 23 '18 23:12

TIMEX


1 Answers

Definition of latitudeDelta:

The amount of north-to-south distance (measured in degrees)

Hence you can simply multiply latitudeDelta by 111 to get the approximate height (which should be the diameter if height > width).

If you need the width, the formula is slightly more complicated as it's dependent on the latitude:

longitudeDelta * 40075 * cos(latitude) / 360

If you need a very precise value of the diameter, you can use the Haversine formula or Vincenty's formulae, which you can implement yourself or use an npm module.
The two points for the formula will be (latitude - latitudeDelta / 2, longitude) and (latitude + latitudeDelta / 2, longitude).

like image 167
Roy Wang Avatar answered Sep 27 '22 22:09

Roy Wang