I have a table in the SQLite database with 4,000 locations, including latitude and longitude. Based on information from the region provided by MKCoordinateRegion class, how can I get the places that are located in this region from the map? Is It Possible?
First I tried using Haversine formula, but what I did was just sort the places closest to the center of the map. But I want places that are located in a region of the map. Or am I wrong?
Thanks in advance.
You can get the max/min values for latitude and longitude from your MKMapView by accessing the region attribute:
MKCoordinateRegion *mapRegion = myMap.region;
You can then access the maximum and minimum values as follows:
maxLatitude = mapRegion.center.latitude + mapRegion.span.latitudeDelta/2;
minLatitude = mapRegion.center.latitude - mapRegion.span.latitudeDelta/2;
maxLongitude = mapRegion.center.longitude+ mapRegion.span.longitudeDelta/2;
minLongitude = mapRegion.center.longitude- mapRegion.span.longitudeDelta/2;
Hope this helps! If you're storing your data in Core Data, an NSSet, or an NSArray, you can then use an NSPredicate to filter out the results that meet the above criteria.
Here's the documentation for your MKMapView and an MKCoordinateRegion.
Are you trying to find all the places in a rectangular region?
Simply set up your query as such:
find all locations where latitude > mapMinLat
and latitude < mapMaxLat
and longitude > mapMinLon
and longitude < mapMaxLon
where the edges of your map are mapMinLat, mapMaxLat, mapMinLon, mapMaxLon.
If you're looking for a polygonal region, you'll need to use a more advanced algorithm.
I have absolutely no idea if this will solve your problem, but SpatialLite is a 3rd party library that says:
SpatiaLite is an open source library intended to extend the SQLite
core to support fully fledged Spatial SQL capabilities.
Looking at the list of functions there is a whole section named SQL functions that implement spatial operators
Rolling your own sqlite code has been answered many times on SO, for example Compiling custom SQLite for an iPhone app
Related
So I have an app with a MapView and I use CoreData to load annotations associated with locations nearby the center of the map. Currently, I define the lat/long range statically like this:
var fetchDataPredicates = [NSPredicate]()
fetchDataPredicates.append(NSPredicate(format: "itemLatitude BETWEEN {%f,%f} AND itemLongitude BETWEEN {%f,%f}", (latitude-0.10), (latitude+0.10), (longitude-0.10), (longitude+0.10)))
However, this is a pretty poor solution. If the user has zoomed out far in the map then the data would only cover a small portion of it.
Does anyone have any good ideas on how to dynamically adjust the lat/long range according to the MapView bounds?
Thanks!
A likely solution would be to ask the map view for its region. That's an MKCoordinateRegion which includes
CLLocationCoordinate2D center
MKCoordinateSpan span
The span includes longitude and latitude deltas, which look like exactly what you need.
I have around 800 geo co-ordinates in my iPhone app as a flat file. I am searching for an effective way to find an algorithm which will take the current user location, loop through all these 800 coordinates and pull only the coordinates which are in 10 miles vicinity. How effectively this can be done? Also please share links which will get me the basic understanding on the maths behind this.
Here is a link for a question where the final code of OP may help you understand the how to create locations from coordinates and how to compute the distance between them.
Here is how to create a location:
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
And here is how to find the distance between two locations:
CLLocationDistance distance = [locationA distanceFromLocation:locationB]; //CLLocationDistance is a double
However you don't have to sort the locations. Just loop through them and add the near locations to an array.
First, I think everyone agrees that to compute distance, you need to use the Haversine function.
Finding the closest point to a given point
If the search time is an issue (iterating over the 800 data points you mentioned) then how about a 2D hash? Simply load the dataset into buckets or regions based on lat/long - then, you won't have to search through the whole data set - only the possible buckets that may contain matches.
Good hash function for a 2d index
I've developed an iPhone application to parse data from an XML file. This data contains a set of longitudes and latitudes for different places.
How do I detect nearest of them according to my current location on the map and how do I set set a range to show places in this range?
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:myLatitude longitude:myLongitude];
CLLocation *myXmlLocation = [[CLLocation alloc] initWithLatitude:xmlLatitude longitude:xmlLongitude];
CLLocationDistance distance = [myLocation distanceFromLocation: myXmlLocation];
This is in meters, so you must do any converting from there. They are also linear, so these aren't driving direction lengths by any means. Good luck.
some trig, but really not too difficult.
this page has an example script, not in your language, but it contains explainations and the equations you need.
http://www.movable-type.co.uk/scripts/latlong.html
and also, please don't write run-on sentences, it took me a while to read your question.
#Vinnie said is absolutely right but these values is air distance.
Also this method is deprecated in iOS 3.2
- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location Parameters
You can't find the distance based on roads with the Apple SDK. Try to use google APIs for finding distance between two points on earth on roads.
hello i want distance between two place ... means i have two places latitude and longitude then i want distance between that two place
i want something like this...
http://maps.google.com/maps?saddr=23.029772,72.527871&daddr=23.1901748,72.0127743
not like php or .net or java function that give calculate distance ...
if i search result by function it give me 55.61 output for this latitude and logitude and in map it give me 65.5 output. how can i get this in json ot xml or any other formate ?
...there's an in-built function in iOS that will do this for you. Check out the CLLocation documentation. Particularly distanceFromLocation:, which as the name suggests returns the difference between two locations in meters. You can create a CLLocation object using lat and lon.
If you don't want a straight line distance, you need to use the Google Directions API, which is well documented here: http://code.google.com/apis/maps/documentation/directions/
In my application i am having 4 places with its longitude and altitude(Given).Now i want to find the nearest place from user.
So anyone can tell me the solution for it or provide me some source code or demo for this.
Thanks to all
Create an CLLocation of the points:
CLLocation* locationx = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
And the compare the points with:
double distance = [userposition distanceFromLocation:locationx];
Keep the nearest location.
Edit:
If you really mean "longitude and altitude" then I dont know how.
just took 10 seconds to ask google and find this, wich describes how to calculate the distance beween two points given latitude and logitude:
http://www.movable-type.co.uk/scripts/latlong.html
all you have to do is calculate the distance to all 4 points and choose the lowest one.
ETDI: i'm sure you're also given the latitude, otherwise you won't even have specific coordinates. if this wasn't a type and you really have to take the altitude into account, take a look at this question at google answers
picknick describes a native class method.
This is probably going to use the Haversine formula (search the web or stackoverflow for discussions) to measure distances.
In my experience, writing your own Haversine is usually faster than using a system/app-provided one, although this is on Windows where COM overhead can be "problematic" as it were!
The Haversine (and many similar geometry-on-the-Earth) formulae can be found here:
http://williams.best.vwh.net/avform.htm
(this is one of my few bookmarks I go back to for reference on a regular basis!)
for example user is at x=0,y=0
Calculate the distance by doing : SquareRoot(x2^2 & y2^2)
You will get a distance. Look through all points and compare which distance is the shortest