3

I have react native application using react-native-maps,in which there is a feature to display near by users based on radius chosen by user that works fine using geocode. but I am facing problem to set zoom level of Map based on user radius? if user selects maximum meter of radius then map should display whole world? How can I achieve this? to set zoom level dynamically based on radius chosen by user?

to set radius used react-native-slider, How can set zoom level on slide of slider?

JPithwa
  • 63
  • 2
  • 6

3 Answers3

0
import { Dimensions} from 'react-native';
let {width, height} = Dimensions.get('window');
const ASPECT_RATIO = width / height;
const LATITUDE_DELTA = 1; //Increase or decrease the zoom level dynamically
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;

<MapView
 region={{
  latitude: currentLocation.latitude,
  longitude: currentLocation.longitude,
  latitudeDelta: LATITUDE_DELTA,
  longitudeDelta: LONGITUDE_DELTA,
 }}>
Ali Hayder
  • 315
  • 1
  • 9
0
      initialRegion:{
        latitude: parseFloat(28.999),
        longitude: parseFloat(28.999),
        latitudeDelta: 0.3822,
        longitudeDelta: Dimensions.get('window').width / Dimensions.get('window').height,
      }
de.
  • 7,068
  • 3
  • 40
  • 69
  • 2
    Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. – Jeremy Caney Jul 04 '21 at 23:42
0

I searched around a lot for this and settled on this function which seems to work really well, if you know the radius of the center point you want to search in, it should generate an initial bounding box:

export const radiusToDelta = (radius: number, lat: number) => {
  // as described here: https://github.com/react-native-maps/react-native-maps/issues/505#issuecomment-354029449
  const oneDegreeOfLatitudeInMeters = 111.32 * 1000;
  const diameter = radius * 2;
  const delta =
    (diameter * 1609.34) /
    (oneDegreeOfLatitudeInMeters * Math.cos(lat * (Math.PI / 180)));

  return {
    latitudeDelta: delta,
    longitudeDelta: delta,
  };
};
Phil Lucks
  • 2,704
  • 6
  • 31
  • 57