-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How get latitudeDelta and longitudeDelta from any region? #505
Comments
I also like to know this too, in the example the latitudeDelta has a value but I don't know how that value was calculated. Thanks |
I didn't figure out how the latitudeDelta and longitudeDelta are calculated. If you want to zoom programmatically on anything, you can use the method Here's my sample of code to center / zoom the map on one of my markers :
This gives me exactly the zoom I want. |
Given an array of coordinates
|
HI needed to pass in an object, not an array and fix a few other variables but latitude is undefined so I am not sure why. do you have any idea? I passed both latitude and longitude. |
Given a point x,y your region is effectively a box around that pint, right? A GPS location has an accuracy field expressed in meters. But a region is expressed in degrees latitude and longitude, so I you need to convert meters to degrees. And because the earth is a sphere the maths (I think) is as follows:
|
Yeah, i thin k there's a divide by zero error that happens at 0 degrees |
|
@npomfret Thank you for this, but I experience that it returns 0 quite often, do you know any way to redeem this? |
Returns 0? I don't understand, this function returns an object, not a number. |
thanks to all! |
@cinder92 Why is there no "official" example on how to calculate delta? I'd never heard of it before this plugin, I'm sure I'm not alone |
@npomfret long/lat-delta algorithm did not work for me an I have my doubts about it's correctness. This is working for me:
|
@cgrossde thanks for the info. Can you give an example of where mine didn't work? It looks ok in my map. I'm using it to draw a circle based on accuracy - the circle is exactly the same size as the one drawn by the native map code it you turn on user tracking. |
Hi @uriklar , can you give the complete example. Here is my code. import React from 'react'; export default class ReactMap extends React.Component {
} const styles=StyleSheet.create({ |
The other functions didn't work for me. @cgrossde's worked partially but I was getting some weird zoom levels when using coordinates in Bogotá, Colombia (it worked with other cities). So I found this simplified version that seems to work everywhere:
|
@cgrossde @supercamilo what does the "distance" refer to? |
@hugoh1995 If I remember correctly it should be meters. |
how do I pass the longitude and latitude received from a firebase database to be rendered in the google map with markers. |
what is this stupid distance, god this delta thing is so frustrating!! |
so, having one {lag,lng} which is center of the map, how could I get (determine) any other {lat,lng} by specify x,y of View of the map? |
what is |
So many magic calculations.. |
Distance is 'accuracy' here. |
Here is the solution for latitudeDelta and longitudeDelta =>
Here is the complete example to view marker with zoom level=>
I hope this will solve your problem. |
returning longitudeDelta:0 0 |
If it's still actual for someone then you can find my solution here, don't know it is super stable or not , but can be useful #705 (comment) |
This solution doesn't work when having location with negative longitude value and another with positive longitude value. |
How i get "distance" ? |
Try following this, worked for me: #356 (comment) |
my simple solution working for me:
|
I am not quite sure what the distance/accuracy is from in the previous answers, for me I have google location details, thus I tried with the following answer, seems to work: But I don't know why in the example code such as https://github.com/react-native-maps/react-native-maps/blob/master/example/examples/AnimatedMarkers.js , the const LATITUDE_DELTA is 0.0922? How is it calculated? Is it the same by details.geometry.viewport.northeast.lat - details.geometry.viewport.southwest.lat? |
|
I created the following based on the mercator math in https://github.com/tuupola/php_google_maps The key function is import React from 'react';
import { useWindowDimensions } from 'react-native';
import MapView from 'react-native-maps';
import { useBottomTabBarHeight } from '@react-navigation/bottom-tabs';
import { useHeaderHeight } from '@react-navigation/elements';
const MERCATOR_OFFSET = Math.pow(2, 28);
const MERCATOR_RADIUS = MERCATOR_OFFSET / Math.PI;
function mercatorLatitudeToY(latitude) {
return Math.round(
MERCATOR_OFFSET -
(
(
MERCATOR_RADIUS *
Math.log(
(1 + Math.sin(latitude * (Math.PI / 180))) /
(1 - Math.sin(latitude * (Math.PI / 180)))
)
) / 2
)
);
}
function mercatorLongitudeToX(longitude) {
return Math.round(
MERCATOR_OFFSET +
(
(
(MERCATOR_RADIUS * longitude) * Math.PI
) / 180
)
);
}
function mercatorXToLongitude(x) {
return (
(
(x - MERCATOR_OFFSET) / MERCATOR_RADIUS
) * 180
) / Math.PI;
}
function mercatorYToLatitude(y) {
return (
(
(
Math.PI / 2
) -
(2 * Math.atan(
Math.exp(
(
y - MERCATOR_OFFSET
) / MERCATOR_RADIUS
)
)
)
) * 180
) / Math.PI;
}
function mercatorAdjustLatitudeByOffsetAndZoom(latitude, offset, zoom) {
return mercatorYToLatitude(mercatorLatitudeToY(latitude) + (offset << (21 - zoom)));
}
function mercatorAdjustLongitudeByOffsetAndZoom(longitude, offset, zoom) {
return mercatorXToLongitude(mercatorLongitudeToX(longitude) + (offset << (21 - zoom)));
}
function mercatorDegreeDeltas(latitude, longitude, width, height, zoom) {
if (!zoom) {
zoom = 20;
}
const deltaX = width / 2;
const deltaY = height / 2;
const northLatitude = mercatorAdjustLatitudeByOffsetAndZoom(latitude, deltaY * -1, zoom);
const westLongitude = mercatorAdjustLongitudeByOffsetAndZoom(longitude, deltaX * -1, zoom);
const southLatitude = mercatorAdjustLatitudeByOffsetAndZoom(latitude, deltaY, zoom);
const eastLongitude = mercatorAdjustLongitudeByOffsetAndZoom(longitude, deltaY, zoom);
const latitudeDelta = Math.abs(northLatitude - southLatitude);
const longitudeDelta = Math.abs(eastLongitude - westLongitude);
return { latitudeDelta, longitudeDelta };
}
// Somewhat arbitrarily, Riverside Park, Independence, KS 67301
const CENTER_UNITED_STATES = {
latitude: 37.24435373025407,
longitude: -95.70234410503208,
};
export default function MapViewWrapper() {
const { width, height } = useWindowDimensions();
const tabBarHeight = useBottomTabBarHeight();
const headerHeight = useHeaderHeight();
const initialRegion = React.useRef(null);
const availableHeight = height - tabBarHeight - headerHeight;
// Only calculate initial region once
if (!initialRegion.current) {
const { latitudeDelta, longitudeDelta } = mercatorDegreeDeltas(
CENTER_UNITED_STATES.latitude,
CENTER_UNITED_STATES.longitude,
width,
availableHeight,
4,
);
initialRegion.current = {
latitude: CENTER_UNITED_STATES.latitude,
longitude: CENTER_UNITED_STATES.longitude,
latitudeDelta: latitudeDelta,
longitudeDelta: longitudeDelta,
};
}
return (
<MapView
initialRegion={initialRegion.current}
style={{ width: width, height: availableHeight }}
/>
);
} There is at least one issue: if you change the zoom from |
This worked for me. Thanks |
Hello, how can i get this requirements ? because without them, my initial marker (when map runs) is in full zoom possible in map, so i need that when the map runs, it have a zoom for 10 or something, is this possible?
UPDATE:
is there any easy way to make zoom when map runs? 😕
Thanks!
The text was updated successfully, but these errors were encountered: