React native MapView: what is latitudeDelta longitudeDelta

React NativeReact Native-Maps

React Native Problem Overview


In the example from react native mapview they have latitudeDelta and longitudeDelta. Is it equal to the height and width? And in what unit? Accoring to their github comment , it says

> Distance between the minimum and the maximum latitude/longitude to be displayed.

But that doesn't help me out. I want to know what the user see on the map.

React Native Solutions


Solution 1 - React Native

I also came here because I couldn't find any good documentation and Googling didn't help. The following is from my own research:

Only one of either latitudeDelta or longitudeDelta is ever used for calculating the size of the map. It takes the larger of the two according to the following formula and ignores the other. This is done to avoid stretching the map.

  1. The map is sized according to the width and height specified in the styles and/or calculated by react-native.
  2. The map computes two values, longitudeDelta/width and latitudeDelta/height, compares those 2 computed values, and takes the larger of the two.
  3. The map is zoomed according to the value chosen in step 2 and the other value is ignored.
    • If the chosen value is longitudeDelta, then the left edge is longitude - longitudeDelta and the right edge is longitude + longitudeDelta. The top and bottom are whatever values are needed to fill the height without stretching the map.
    • If the chosen value is latitudeDelta, then the bottom edge is latitude - latitudeDelta and the top edge is latitude + latitudeDelta. The left and right are whatever values are needed to fill the width without stretching the map.

Here are some examples:

// this uses latitudeDelta because 0.04/300 > 0.05/600
<MapView
  style={{width: 600, height: 300}}
  region={{
    latitude: 31.776685,
    longitude: 35.234491,
    latitudeDelta: 0.04,
    longitudeDelta: 0.05,
  }}
/>



// this uses longitudeDelta because 0.05/600 > 0/myVar when myVar > 0
<MapView
  style={{width: 600, height: myVar}}
  region={{
    latitude: 40.689220,
    longitude: -74.044502,
    latitudeDelta: 0,
    longitudeDelta: 0.05,
  }}
/>

Solution 2 - React Native

i found this in apple developer guide

> The amount of east-to-west distance (measured in degrees) to display for the map region. The number of kilometers spanned by a longitude range varies based on the current latitude. For example, one degree of longitude spans a distance of approximately 111 kilometers (69 miles) at the equator but shrinks to 0 kilometers at the poles.

ļ˜€ there have an issue in GitHub too for that library that have been resolved
https://github.com/react-community/react-native-maps/issues/637

this visual explanation also help stackoverflow.com
in this Wikipedia article it shows how to calculate exact value
https://en.wikipedia.org/wiki/Latitude#Length_of_a_degree_of_latitude

Solution 3 - React Native

You need

"centroid": {
    "latitude": "24.2472",
    "longitude": "89.920914"
},
"boundingBox": {
    "southWest": {
        "latitude": "24.234631",
        "longitude": "89.907127"
    },
    "northEast": {
        "latitude": "24.259769",
        "longitude": "89.934692"
    }
}

or similar data to calculate latitudeDelta and longitudeDelta properly.

Below is a demo code for react/react-native.

componentDidMount() {
    const { width, height } = Dimensions.get('window');
    const ASPECT_RATIO = width / height;

    const lat = parseFloat(centroid.latitude);
    const lng = parseFloat(centroid.longitude);
    const northeastLat = parseFloat(boundingBox.northEast.latitude);
    const southwestLat = parseFloat(boundingBox.southWest.latitude);
    const latDelta = northeastLat - southwestLat;
    const lngDelta = latDelta * ASPECT_RATIO;

    this.setState({
      region: {
        latitude: lat,
        longitude: lng,
        latitudeDelta: latDelta,
        longitudeDelta: lngDelta
      }
    })     
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthatsITView Question on Stackoverflow
Solution 1 - React NativeMike MartinView Answer on Stackoverflow
Solution 2 - React Nativenima moradiView Answer on Stackoverflow
Solution 3 - React NativeM. SihanView Answer on Stackoverflow