I am developing an iPhone app. here in my App I have included Google maps.I am showing the highlighted path in map.
Now my Question is how to calculate distance between two point as per path as Apple"s native map App is showing routes with distance.
Thanks in Advance.
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.latitude];
CLLocationDistance distance = ([loc2 distanceFromLocation:loc1]) * 0.000621371192;
This may help you.
When running in the simulator, Core Location assigns a fixed set of coordinate values to this property. You must run your application on an iOS-based device to get real location values.
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.latitude];
Related
I need to calculate the distance between two points in iOS. I can guarantee at least an iPhone 4 so the picture quality on the camera should be good. The idea is to calculate the distance to a point using the picture. There's an app called easyMeasure which does exactly what I need to do.
I'm ok with Pythagoras but this boggles my mind. How would I do something like this?
Ok, so you were correct in that you need to use sine and such. First though, you'll need to find the lens angle of the iPhones camera. Do do this, put the camera a known distance away from the wall and measure how far it is from the edge of the field of vision to the other side and divide by two. To find θ in the picture below, use tanθ = opposite/adjacent, so inverse tan(opposite/adjacent) = θ.
Once you know that, you just have the user take a picture, and give a measurement for how big something on the screen really is. Then just use tanθ = opposite/adjacent, and since you now know θ and the opposite distance, adjacent = opposite/tanθ.
Hope that helps!
New update in ios7
#import CoreLocation;
#import MapKit;
CLLocation *sanFrancisco = [[CLLocation alloc] initWithLatitude:37.775 longitude:-122.4183333];
CLLocation *portland = [[CLLocation alloc] initWithLatitude:45.5236111 longitude:-122.675];
CLLocationDistance distance = [portland distanceFromLocation:sanFrancisco];
MKDistanceFormatter *formatter = [[MKDistanceFormatter alloc] init];
formatter.units = MKDistanceFormatterUnitsImperial;
NSLog(#"%#", [formatter stringFromDistance:distance]); // 535 miles
I am making google map integration in iphone. I have to show current location showing in map. How will i get current location ?
Thanks
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
Search for Accessing the Device’s Current Location in following article:
MKMapView Class Reference
showsUserLocation (MapView.showsUserLocation = YES;)
userLocationVisible
userLocation
Related SO posts:
iPhone current user location coordinates showing as (0,0)
How to view the current location in google map using longitude and latitude of that location in Objective C
I think the question has been answered -
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
place this in the viewdidappear:animated
then you should find that you have the right location. Doing it any sooner and the location won't be set yet and you'll probably end up in the ocean :)
The short answer is to write code like mapview.showsUserLocation = YES;, where mapview is your UIMapView class object.
I am using CLLocation to work out the distance from the current user location, and an annotation. However I just wanted to know if this would be correct. I am currently using iPhone Simulator for this and according to the MKMapView the iPhone Simulator is situated here:
Lat: 0 Long: -1067024384
The annotation's position is:
workingCoordinate.latitude = 40.763856;
workingCoordinate.longitude = -73.973034;
However if you take a look in google maps you will find out how close these distances are, yet so far apart according to CLLocation. I am using the following code to determine the distance between them both.
CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance dist = [loc distanceFromLocation:loc2];
int distance = dist
NSLog(#"%i", distance);
The distance being NSLogged is 12769908. I believe that this is incorrect, and therefore there must be a problem with my code.
If there is please can you point it out!
You have two bad habits.
You should not depend on simulator in situations need hardware censor status. Especially when you want correct test.
You're handling types incorrectly. So you can't check values correctly. How is the longitude -1067024384? Longitude value is degrees. This means it's valid range is limited -90.0 ~ +90.0 by definition of longitude.
Your longitude value is out of range. This means one of these. You printed the value wrongly or the real value was wrong. Simulator can print wrong value. Or you printed the value with wrong method. You have to try:
Test on real device which has real hardware censors.
If bad result continues after that,
Review ALL of your application code.
Especially for printing, handling values. Check you're using correct types and castings in > each situations. Because you may did buggy operation in somewhere habitually.
And also, I recommend checking all of intermediate values like this.
CLLocationCoordinate2D annocoord = annotation.coordinate;
CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate;
NSLog(#"ANNO = %f, %f", annocoord.latitude, annocoord.longitude);
NSLog(#"USER = %f, %f", usercoord.latitude, usercoord.longitude);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
NSLog(#"LOC = %f, %f", loc.coordinate.latitude, loc.coordinate.longitude);
NSLog(#"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude);
CLLocationDistance dist = [loc distanceFromLocation:loc2];
NSLog(#"DIST: %f", dist); // Wrong formatting may show wrong value!
Try #"%f" and don't cast it that way.
In CLLocation.h
typedef double CLLocationDistance;
I was using following code to find distance between 2 points:
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:28.3636 longitude:-77.1212];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:42.353297 longitude:-71.578858];
float target = [location1 getDistanceFrom:location2];
But now getDistanceFrom is deprecated by Apple.
What should I try to perform same task?
How about -distanceFromLocation: instead? It's even explicitly mentioned in the documentation on -getDistanceFrom: as the method to use as a replacement.
You want to use 'distanceFromLocation:'. See the documentation for more: http://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html
I am a new iPhone developer learning Objective-C, and am trying to dynamically calculate the distances between the users latitude/longitude coordinates, with latitude/longitude coordinates in a SQLite table. I know that we can use CLLocations method:
(CLLocationDistance)distanceFromLocation:(const CLLocation *)location
to do this, but I'm not sure how to use it given the data that I have. How does one use the above method using pairs of latitude/longitude coordinates, considering the above method only deals with location objects of type CLLocation? Can anyone give me a simple example of how to use this method using two pairs of latitude/longitude coordinates?
Just create CLLocation objects from your data:
// Assumption: lat1, lon1 and lat2, lon2 are double values containing the coordinates
CLLocation *firstLocation = [[[CLLocation alloc] initWithLatitude:lat1 longitude:lon1] autorelease];
CLLocation *secondLocation = [[[CLLocation alloc] initWithLatitude:lat2 longitude:lon2] autorelease];
CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation];