Get true coordinates from scrolled leaflet map - leaflet

I'm using React Leaflet. When a user clicks on the map, I retrieve the coordinates and a marker is set on that. Everything works on the map, but the map is used as a way to retrieve weather data for the set location.
Explanation
Here is a picture which might help illustrate the problem I'm facing
At the top I added longitudes values for each "piece" of earth.
There is a marker with "true" longitude which is -0.09°
On the left earth, the same location would return -360.09° longitude
On the right earth, the same location would return 359.91° longitude
I retrieve latitude and longitude with this piece of code:
setMarker = e => {
this.props.updateLocation({
lat: e.latlng.lat,
lng: e.latlng.lng,
})
}
The problem
The problem I'm facing is, if I pass down a longitude value to the weather API thats out of the allowed ranges (> -180° && < 180°), it doesn't return anything, cause that longitude, of course, doesn't exist.
Is there a method that returns the "true" latitude and longitude?
If this doesn't exist, how can I disable the map scrolling (allow only one earth to appear)? Or just allow placing a marker inside the first earth? I am unsure what is the best approach for this, and the leaflet documentation is confusing me as I don't understand some of the terminology.

After more searching I have found a built in method that does exactly what i want it to.
Found it on this GitHub issue https://github.com/Leaflet/Leaflet/issues/1885#issuecomment-91395167
To get actual latitude and longitude on a mouse click, no matter on which earth you click, you use the wrap() method on latlng:
event.latlng.wrap()
This returns an object, e.g.
{
lat: 63.51385990617828,
lng: 173.671875
}

Related

How can I query the feature that's closest to the geocode result in Mapbox?

I'm trying to create a Mapbox map with thousands of points. I want the user to be able to type an address and a div to be filled with information from the closest point to that address. I'm using the mapbox-gl-geocoder control
What would be perfect is if I could use something like:
geocoder.on('result', function(e) {
var features = map.queryRenderedFeatures(e.result.center)
});
But queryRenderedFeatures doesn't accept latlong coordinates, it requires viewport coordinates.
The next thing I tried was
map.on('moveend', function () {
var xwindow = window.innerWidth / 2;
var ywindow = window.innerHeight / 2;
var xywindow = {'x': xwindow, 'y': ywindow};
var features = map.queryRenderedFeatures(xywindow);
});
That gives results that are near the queried address, but not the closest point. For instance if I geocode an address where I know a point exists, this query will return a different point a few streets away.
Is there a way to use queryRenderedFeatures to get a feature on the location that is returned by the geocode control?
But queryRenderedFeatures doesn't accept latlong coordinates, it requires viewport coordinates.
Yes but you can use map.project that Returns a Point representing pixel coordinates, relative to the map's container, that correspond to the specified geographical location.
Once you have all the features you'll need to get the distance to each of them to find the closest. cheap-ruler would be a good choice for that.
An index like geokdbush probably doesn't make sense here as you're only running it once.

Extract Latitude and Longitude of a marker placed on the map using click_for_marker from the folium package

I am using Folium package in Python with click_for_marker.
Once I click on the map, it displays the latitude and longitude in the popup. I want to extract that latitude and longitude and use it to draw a circle of certain radius using the circle_marker function in python.
How exactly can I extract this latitude and longitude information obtained from click_for_marker and making the whole experience dynamic where when ever I click on the map, it needs to display a circle of certain radius?
kindly help.

How to find if a long/lat point is in the visible bing map

This should be quite simple but I am not getting it.
I have a database of locations with lon/lat specified.
After loading the bing map I get the bounds
var view = map.getBounds();
and then call a webmethod to get all the locations which should be shown (within the bounds of the visible map).
I cannot figure out a query to get the locations (which all have a lon/lat specified) .
This obviously does NOT work as when negative values come into play they mess up the query:
SELECT Location_name, longtitude, latitude FROM location_view WHERE latitude< '40.112' and latitude> '35.783' and longtitude< '28.10453' and longtitude> '19.315'
Is there a normalized way to do this? So the comparison would work?
Your query will work absolutely fine with negative values: a longitude of -130 is still west of a longitude of -120. The only situation in which it won't work is if the bounds of your map crosses the 180th meridian. I.e. the "westmost" longitude is 170 and the "eastmost" latitude is -170.
What database are you using? If you're using SQL Server then you can define each of your locations as a Point using the geography datatype. The geography datatype operates on a round model of the earth, so it will account correctly for crossing the 180th meridian with a query like this:
SELECT Location_name
FROM location_view
WHERE location.STIntersects('POLYGON((19.315 35.783, 28.10453 35.783, 28.10453 40.112, 19.315 40.112, 19.315 35.783))') = 1;

MKMapKit true distance between two annotations

I want to make a application that calculates the distance between the user's current location and the nearest something (store, whatnot). Is there any way to obtain the real distance (following public roads ) instead of a direct distance (line from point A to B) ??
Thank you
Use google API to get the route and distance:
http://code.google.com/apis/maps/documentation/directions/
I don't think you can get the route (following roads) from MKMapKit.
The google maps API will return the route (following roads) as a set of latitude and longitude coordinates. It will also return the calculated distance.

Why does a pin placed at the same latitude and longitude in MKMapView differ from one in Google Maps?

I drop a pin in an MKMapView with longitude and latitude. When I place same longitude and latitude on google map the pin on both are slightly different.
I have a web/iPhone app that renders a map for positions sourced from the same database and haven't seen this issue. I would check your assumptions:
Are you truncating your values over the wire?
Are you rounding the position values differently on the two platforms?
Check the values you are passing into Google Maps and MapKit by logging them and comparing.
You can see from the following screenshots that the same latitude/longitude are rendered on the same position on the map:
Update
I tried the coordinates you mentioned in the comments and got the same position for both. You need to check your assumptions. The values that you are using on the web or on the device are not the values you think they are: