how can i get distance bettween two place? - iphone

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/

Related

How to get the coordinates of the polygon area

How to get the coordinates of the polygon area:
http://wikimapia.org/#lang=en&lat=49.964473&lon=36.262436&z=12&m=b&show=/20421408/Chervonozavodsky-district
You need to use the place.Search function of The Wikimapia API and enter the latitude, longitude and the name of the place as parameters.
The API returns a lot of data separated in blocks. The one you are interested in is the geometry.
You can make tests with the Wikimapia API to filter the results to your needs. With the lat lon parameters and the district name you provided I was able to get the area you needed as the first result.
The places[0].polygon is what you need. A JSON array of coordinates.
Here´s the url I used to get the results:
http://api.wikimapia.org/?key=example&function=place.search&q=Chervonozavodsky-district&lat=49.964473&lon=36.262436&format=json&pack=&language=en&data_blocks=geometry%2C&page=1&count=1
Note that in this example i made the request for a JSON result. But you can also ask for a XML if you prefer.
Hope it helps!

Find nearby locations in a given region

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

Find coordinates nearest to my current GPS location

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

IOS Core location - Find out nearest branch

I have create a program to find out my current location using IOS corelocation framework.It works fine.
I need to create a program that list nearest branchaes of a shop chain, while a user travel with the phone.My data base contains the branches details with Latitude and lognitude.How i compare with these details to find out the nearest branch.
Help is highly appreciated.Anybody knows any example program
Thanks,
VKS
You can make use of distanceFromLocation: method of CLLocation.
distanceFromLocation:
Returns the distance (in meters) from the receiver’s location to the
specified location.
(CLLocationDistance)distanceFromLocation:(const CLLocation
*)location
Parameters
location
The other location.
Return Value
The distance (in meters) between the two locations.
Discussion
This method measures the distance between the two locations by tracing
a line between them that follows the curvature of the Earth. The
resulting arc is a smooth curve and does not take into account
specific altitude changes between the two locations.
Availability
Available in iOS 3.2 and later.
By using this method to get the distances of the shops with your current location, you can then sort the list based on the distance to get the nearest branch
VKS, I got to solve the exact same problem too now (using apptarget iOS 5). Do you have any example code / snippets / tips to share for this problem, that I assumed you solved a long time ago? The above is very good explanation from 7KV7, but some examples will speed up my process a lot. Thank's :-)

How To calculate nearest location from user?

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