Get lat/lng center of country, state or district - openstreetmap

Given is a DB full of parent/child relationships of political district geo data/names:
Country
State
District
Now I'm querying for that data, building a string like germany,bavaria,ebersberg and then I try to fetch the lat/lng center of that district via the Nominatim API (part of OpenStreetMaps).
Example String:
http://nominatim.openstreetmap.org/search?format=xml&polygon=0&addressdetails=0&q=germany,bavaria,ebersberg
Problem is, that I get back a bunch of POIs with lat/lng instead exact geographical data. This often results in having exact the half of that district displayed as the first POI could be close to that districts border.
Does anyone know of a way to get the lat/lng of the center or how to center a OSM map? OR does anyone know of an alternate API that can achieve this and tell how it works/make an example?

Does anyone know of a way to get the lat/lng of the center or how to center a OSM map?
When you get a bunch (list, set) of points of interest with latitudes and longitudes, you have a feature, not a problem.
You iterate through the POI, building a bounding rectangle that includes all of the POI. In other words, you find the minimum latitude, minimum longitude, maximum latitude, and maximum longitude.
You find the center of the rectangle by the formula
(maximum + minimum) / 2
The bounding area should be small enough that you're not going to incur much error working with latitude and longitude.
Otherwise, you can calculate the distance between the minimum latitude, longitude and the maximum latitude, longitude. The center is half the distance from the minimum latitude, longitude. Convert that center point back to a latitude and longitude for your answer.

Related

Hey are coordinates from Apple Maps incorrect when used as a CLLocation in mapkit?

I’m working on an app that shows various places on a map (SwiftUI using mkmapkit in a uiViewRepresentable) and have found that coordinates are being placed off target for some reason.
I have the data listed in Firebase as a map - Latitude value and Longitude value that is then transferred into a CLLocation in app.
Example of the problem is that Big Ben in London should be (according to Apple Maps) be at 51.50070N and 0.12455W doesn’t appear there in app (see image) but the app reports coordinates of 51.500702, 0.124610 - when tapped just below the annotation.reported coordinates
The sign on the longitude is incorrect. According to my MKLocalSearch (and confirmed by dropping an annotation there), Big Ben is at a latitude of 51.5006854 and a longitude of -0.1245698. Note, that's -0.1245698 (aka, 0.1245698W) and not 0.1245698 (aka 0.1245698E).
A longitude is defined “relative to the zero meridian, with positive values extending east of the meridian and negative values extending west of the meridian.”
But your map is showing the coordinate east of the meridian, not the one west of the meridian. Here are the two coordinates:
And zooming in on that incorrect coordinate to the east, that's obviously where your map is pointing:
But Big Ben is to the west of the meridian:

Calculate proper latitudeDelta and longitudeDelta based on radius?

Does anyone know of a mathematical way to calculate the proper latitudeDelta/longitude delta of a MapView region based on search radius? For instance, I give my users the option to search for items within a radius of 5, 10, 25, 50 & 100 miles. I would like the MapView region to accurately reflect that search radius and just show enough "map" to encompass the search radius.
Forget about the latitude and longitude. Just convert from miles to meters and then create an MKCoordinateRegion by calling init(center:latitudinalMeters:longitudinalMeters:), where the latitudinalMeters and longitudinalMeters are twice the radius, and use that, centered at the user's location, to set the map view's region.

Setting up coordinates for NYC

I have a project which involve me to map some random objects in NYC(Astoria Park, etc).
Now I'm using openalayer3 and osm to display a map, but I having a hard time understanding how to calculate coordinates for NYC.
I have made a map, but it is focused on city of London:
var city = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326', 'EPSG:3857');
Coordinates for Astoria Park(40.780229N, 73.920935W), but obviously this are not the coordinates that I'm locking for, I have read about ol.proj.transform, but I can't understand how to calculate this.
Can someone explain me how can I overcome this and learn something, thank you.
OpenLayers uses coordinates in the form of longitude, latitude. 40.780229,-73.920935 are the correct coordinates for Astoria Park, New York, however in latitude, longitude. Try var city = ol.proj.transform([-73.920935, 40.780229], 'EPSG:4326', 'EPSG:3857'); instead.

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;

Need help with finding nearby location

I am working app which need to find nearby distributors of particular product. As of now I have current locations latitude and longitude. Besides that I also have list of all product distributors with their respective coordinates. I am running query which gives me nearest 10 locations but for that it goes thorough every record in DB calculate distance between current and that particular location. It takes way too much time. Is there any other alternative that can I take ?
Can you not first narrow down the data set by creating a max and min long and lat (say within 10 miles of the current location). You can then query the data set by lat > minLat and lat < maxLax, etc. You could then sort them as you are proposing by calculating the actual distances on the reduced subset if you need too.
To avoid calculating distance on every location you could create a lat long box (lets say for 10 miles) using the max top left lat long (10 miles up and 10 miles left) and the max bottom right lat long (10 miles down and 10 miles right). Then your query will find lat longs in that box using >= and <= and then calculate the distance for each of those to filter out the locations in the corners which exceed 10 miles.
Another other option is to look into spatial indexing for SQLite.
You can narrow down list of location by creating rectangular buffer around your location, to filter locations that are nearby.
SELECT * FROM table t
WHERE t.lat<(lat+buff) AND t.long<(long+buff) AND t.lat>(lat-buff) AND t.long>(long-buff)
lat, long - your location, buff - some value you can adjust to match your app need (e.g. 100
feet, 1 mile, etc)
Then you can ran your distance calculation on returned records.