Calculate distance between two locations using google map in iphone - iphone

I just want to calculate distance between two location .
In which user can enter both the location's addresses, and from that addresses I want to calculate the distance between them.
Is it possible to calculate distance from CLLocation using these addresses ?

First you need to geocode the address to latitude/longitude, then you can use the CLLocation framework to calculate the distance.
To geocode the adress, you could use this forward geocoding API.
// get CLLocation fot both addresses
CLLocation *location = [[CLLocation alloc] initWithLatitude:address.latitude longitude:address.longitude];
// calculate distance between them
CLLocationDistance distance = [firstLocation distanceFromLocation:secondLocation];

Related

GPS how to combine latitude/longitude with horizontalAccuracy to create "buckets" of data?

I'm processing user photos on an iPhone and some of them are geotagged (have latitude and longitude). For example, my longitude looks like -73.123456. Each GPS tag has a horizontalAccuracy property, expressed in meters.
How can I combine the horizontal accuracy with latitude/longitude to create groups(buckets) of GPS coordinates?
For example, I want to have all photos that are taken around the user's house to fall into one bucket, while all photos taken around work fall into another bucket.
I originally was thinking about multiplying the latitude/longitude by 1,000,000 , then cutting off fractional components:
CLLocation *assetLocation = [asset valueForProperty:ALAssetPropertyLocation];
CLLocationCoordinate2D coordinate = [assetLocation coordinate];
NSInteger latitudeInt = coordinate.latitude * 1000000;
NSInteger longitudeInt = coordinate.longitude * 1000000;
latitudeInt = latitudeInt-(latitudeInt % 100000);
longitudeInt = longitudeInt-(longitudeInt % 100000);
NSLog(#"trimmed:(lat %f, lon %f) real: (lat %f, lon %f) ", latitudeInt/1000000.0, longitudeInt/1000000.0,coordinate.latitude,coordinate.longitude);
This will result in GPS reading of -73.1
The problem with this approach is that for one photo I get GPS location that is 1km off, but if I subtract 0.099 from the latitude/longitude reading, the error can be up to 5kilometers. Ideally, my buckets would each be 1-2 kilometers across, centered around some GPS coordinate.
This poses a question - how can I combine information about latitude/longitue, together with horizontal accuracy to create groups/buckets of GPS coordinates? They don't have to be accurate or centered directly onthe user's house, but I would like all photos taken around the users's house to fall into the "user's home" bucket.
You should look into the k-means clustering Algorithm.
Your task is a quite difficult, this will cost a lot of time and needs algoritmic (geo-spatial) knowledge. Think again if you need that.

plot pins on MKMapView around 10 kms from current location

I have an MKMapView & I am using PinAnnotation for plotting the pins on MKMapView.
I have a local database which contains the latitude & longitude of thousands of locations. I want to plot the pins around 10 kms from current location on MKMapView, when the application is launched.
It depends what your database. A simplistic way would be to work out the longitude difference for 10km (it is different at the equator and near the north pole) and the latitude difference for 10km, then search your database for everything between maxLat and minLat and maxLong and minLong. That gives you items in a rectangle but it's a start. From there you could cycle through every result, make it a CLLocation and check it is within 10km using distanceFromLocation.
I done it using the following way -
1) First found the current latitude & longitude using the delegate method of MKMapView(didUpdateUserLocation).
2) Found the distance from the current location with the location stored in my local DB in KM.
3) If distance <=10 Kms, then plot pin, else do nothing.

How to calculate distance to locations

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.

How to know distance between places

Hello I am creating travel app. I want to find current location and find the distance between selected hotel or place from this location.. I searched core location but it is not returning longitude and latitude of current location in simulator .please help me and how can I calculate distance ?
To calculate distances you can use the distanceFromLocation: method of the CLLocation class.

Calculate distance from Location - iPhone

I'm trying to calculate the distance between two places user Core Location. I've found a few posts that state to use
-(CLLocationDistance)distanceFromLocation:(const CLLocation *)location
Found some other test code in the thread below:
CLLocationDistance NaN
I'm not sure how to put it all together, to get the result I want ?
Anyone any thoughts ?
Regards,
Stephen
If you have a location named myLocation and want to find the distance from another location, say, restaurantLocation, then it seems you would do something like
CLLocationDistance distance = [myLocation distanceFromLocation:restaurantLocation]
This will give you the distance, in meters, between the two.