I am using this function to get the distance from one CLLocation to another
here venue.lat is NSNumber
GeoAddress *address = [[GeoAddress new] init];
CLLocationCoordinate2D coord;
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];
address.latlng = [[CLLocation alloc] initWithLatitude:coord.latitude longitude: coord.longitude];
if (curUserLocation.coordinate.latitude){
address.distance = [address.latlng distanceFromLocation:curUserLocation];
NSLog(#" DISTANCE %f", address.distance);
NSLog(#" LOC1 %f lat %f lng", address.latlng.coordinate.latitude, address.latlng.coordinate.longitude);
NSLog(#" ME %f lat %f lng", curUserLocation.coordinate.latitude, curUserLocation.coordinate.longitude);
}
This is Geoaddress:
// GeoAddress.h
#interface GeoAddress : NSObject
{
CLLocation * latlng;
CLLocationDistance distance;
}
#property CLLocationDistance distance;
#property (nonatomic, retain) CLLocation * latlng;
// GeoAddress.m
#import "GeoAddress.h"
#implementation GeoAddress
#synthesize distance;
#synthesize latlng;
This is the log:
DISTANCE 15106456.105786
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng
The numbers are completely off actual distance is ~0.17 km
What am I doing wrong??
You have lines that say:
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];
Obviously that should be:
coord.longitude = (CLLocationDegrees)[venue.lng doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lat doubleValue];
Where you set coord from venue, the latitude and longitude are being assigned backwards.
This gives you the unexpected distance.
Look at your output:
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng
You exchanged lat and long in ME
Related
I have create a zone on the map and save this coordinate and current zoom level in the database. When we edit this zone and open the map in the mapkit with the same coordinate and zoomlevel region on the map shown is different what we have created.
CLLocationCoordinate2D cd= CLLocationCoordinate2DMake(30.724300, 76.723166);
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(cd, 4640, 4565);
[self.mapView setRegion:viewRegion animated:YES];
Using this function I get the region width and height
-(void) getMapDelta:(CLLocationCoordinate2D) coord
{
MKCoordinateSpan span = self.mapView.region.span;
CLLocationCoordinate2D loc = self.mapView.region.center;
//get latitude in meters
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:(self.mapView.region.center.latitude - span.latitudeDelta * 0.5) longitude:self.mapView.region.center.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:(self.mapView.region.center.latitude + span.latitudeDelta * 0.5) longitude:self.mapView.region.center.longitude];
//get longitude in meters
CLLocation *loc3 = [[CLLocation alloc] initWithLatitude:self.mapView.region.center.latitude longitude:(self.mapView.region.center.longitude - span.longitudeDelta * 0.5)];
CLLocation *loc4 = [[CLLocation alloc] initWithLatitude:self.mapView.region.center.latitude longitude:(self.mapView.region.center.longitude + span.longitudeDelta * 0.5)];
int metersLatitude = [loc1 distanceFromLocation:loc2];
int metersLongitude = [loc3 distanceFromLocation:loc4];
NSLog(#"Delta> %d / %d", metersLongitude,metersLatitude);
NSLog(#"Coor> %f / %f", coord.latitude,coord.longitude);
}
I'm trying to calculate and return the distance between two points and I'm getting a ridiculous number. I've tried both a user-defined haversine method and the distanceFromLocation method and they are both returning 10282657.652... for the distance between lat = 32.60641072/ lon = -85.48713530 and lat = 32.605638 and lon = -85.486890. Are these distances too close?
CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.latitude];
CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:_head.lattitude longitude:_head.longitude];
double distance = [startLocation distanceFromLocation:endLocation];
CLLocationCoordinate2D newCoordinate = [startLocation coordinate];
CLLocationCoordinate2D oldCoordinate = [endLocation coordinate];
double distance2 = [self distanceFrom:newCoordinate to:oldCoordinate];
NSNumber *myDoubleNumber = [NSNumber numberWithDouble:distance2];
_charLabel.text = [myDoubleNumber stringValue];
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:lon2];
CLLocationDistance distance= [location1 distanceFromLocation:location2]*1.09361;
"distance" will return on meter.
This code worked for me :)
This should be Los Angeles:
double latitude = 34.05;
double longitude = 118.25;
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latitude, longitude);
NSString *desc = #"hey there";
NSString *address = #"some address";
CLLocationCoordinate2D coordinate;
coordinate.latitude = location.latitude;
coordinate.longitude = location.longitude;
MyLocation *annotation = [[MyLocation alloc] initWithName:desc address:address coordinate:coordinate]; //implements <MKAnnotation>
NSArray *annotations = #[annotation];
[self.mapView showAnnotations:annotations animated:YES];
However this annotation ends up in Eastern China. What am I missing in the conversion?
I think you want -118.25 degrees for Los Angles.
My app loads the user's past polylines and displays them on the map. Then the app starts tracking and a straight line is drawn from the last updated coordinate to the first, thereby connecting the two separate lines when they should be separate (shown here
I want to remove this straight line, so I'm thinking the easiest way would be to discard coordinates that are outside a set time frame (e.g 1 minute), so polylines on the map remain separate. I'm not sure how to do this though... I'm a beginner so any suggestions would be much appreciated!
I use this code when loading the past polyline
(IBAction)didClickLoadCoordinates:(id)sender {
// get a reference to the appDelegate so you can access the global managedObjectContext
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:#"Route"];
NSError *error;
id results = [appDelegate.managedObjectContext executeFetchRequest:request error:&error];
if ([results count]) {
polyLine = (Route *)(results[0]);
NSArray *coordinates = polyLine.coordinates;
int ct = 0;
for (CLLocation *loc in coordinates) {
NSLog(#"location %d: %#", ct++, loc);
}
// this copies the array to your mutableArray
_locationsArray = [coordinates mutableCopy];
}
NSInteger numberOfSteps = _locationsArray.count;
//convert to coordinates array to construct the polyline
CLLocationCoordinate2D clCoordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_locationsArray objectAtIndex:index];
CLLocationCoordinate2D coordinate2 = location.coordinate;
clCoordinates[index] = coordinate2;
}
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:clCoordinates count:numberOfSteps];
[_mapView addOverlay:routeLine];
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
And this code to just normally update the cllocations and make the polyline
//get the latest location
CLLocation *currentLocation = [locations lastObject];
//get latest location coordinates
CLLocationDegrees latitude = currentLocation.coordinate.latitude;
CLLocationDegrees longitude = currentLocation.coordinate.longitude;
CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(latitude, longitude);
//zoom map to show users location
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 2000, 2000);
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];
//store latest location in stored track array
[_locationsArray addObject:currentLocation];
//create cllocationcoordinates to use for construction of polyline
NSInteger numberOfSteps = _locationsArray.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_locationsArray objectAtIndex:index];
CLLocationCoordinate2D coordinate2 = location.coordinate;
coordinates[index] = coordinate2;
}
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[_mapView addOverlay:routeLine];
NSLog(#"%#", _locationsArray);
I have currently this setup and I would love to change from Km to Miles in calculating distance. Here's my current code:
CLLocation *userloc = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
CLLocationDistance distance2 = [userloc distanceFromLocation:loc]/ 1000;
NSLog(#"%f",distance2);
[distance setText:[NSString stringWithFormat:#"%.2f km", distance2]];
}
It's a simple conversion:
CLLocationDistance distance2 = [userloc distanceFromLocation:loc] * 0.000621371;
NSLog(#"%f",distance2);
[distance setText:[NSString stringWithFormat:#"%.2f mi", distance2]];