Can't calculate distance to annotation - ios

Trying calculate distance to annotation, but for some reason can't get annotation coordinates. I load annotations from plist.
CLLocationCoordinate2D annocoord = myAnnotation.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:myAnnotation.coordinate.latitude longitude:myAnnotation.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);
Log:
ANNO = 0.000000, 0.000000
USER = 60.492631, 22.258933
LOC = 0.000000, 0.000000
LOC2 = 60.492631, 22.258933
DIST: 6999960.693412

-(CGFloat)distanceBetweenTwoPoints:(CGPoint)firstP secondPoint:(CGPoint)secondP{
CGFloat distance = sqrtf(powf((secondP.y - firstP.y), 2.0) + powf((secondP.x - firstP.x), 2.0));
return distance;
}
Use this code to find distance between two points.

Related

How to Specify the Exact Region on the Map in IOS

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);
}

In IOS track user location in Google Map

I am using google map SDKs in my iOS project to track the user location and a draw a route line of the user path for his movement.I want to add two images for the starting point of the user and another for user movement. I can't add these images.Please help me to add these images. Here is my code:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//get the latest location
CLLocation *currentLocation = [locations lastObject];
//store latest location in stored track array;
[self.locations addObject:currentLocation];
//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, 1000,1000);
MKCoordinateRegion adjustedRegion = [mapview regionThatFits:viewRegion];
[mapview setRegion:adjustedRegion animated:YES];
NSInteger numberOfSteps = self.locations.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [self.locations objectAtIndex:index];
CLLocationCoordinate2D coordinate2 = location.coordinate;
coordinates[index] = coordinate2;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[mapview addOverlay:polyLine];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor redColor];
polylineView.lineWidth = 10.0;
return polylineView;
}
This code draw the user path. But I want this type of output.
Someone please help me.
Add annotations to mapview, for first and last objects of your self.locations array. You will also need to remove the old annotation each time the location is updated to a new one.
Let centerY and centerX be the center point for the image
CLLocationDegrees offset = 0.01; // change size here
CLLocationDegrees southWestY = centerY - offset;
CLLocationDegrees southWestX = centerX - offset;
CLLocationDegrees northEastY = centerY + offset;
CLLocationDegrees northEastX = centerX + offset;
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(southWestY,southWestX);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(northEastY,northEastX);
GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest coordinate:northEast];
GMSGroundOverlay *overlay = [GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:[UIImage imagedNamed:#"YourImage.png"]];
overlay.bearing = 0;
overlay.map = self.mapView
With this approach (unlike with the marker) the image will zoom in and out as the user zooms in/out of the map.

Why is the distanceFromLocation: method returning a ridiculous number

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 :)

needs to calculate distance between two location [duplicate]

This question already has answers here:
Calculate distance between two place using latitude-longitude in gmap for iPhone [duplicate]
(2 answers)
Closed 8 years ago.
I tried to calculate the distance between two locations but something is wrong because the distance is 0 every time.
float oldLatitude = [gInfo.latitude floatValue];
float oldLongitude = [gInfo.longitude floatValue];
CLLocation *oldLocation = [[CLLocation alloc] initWithLatitude:oldLatitude longitude:oldLongitude];
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:newLatitude longitude:newLongitude];
if (newLatitude == 0 && newLongitude == 0) {
[self showAlarm2];
return;
}
float distance = [newLocation distanceFromLocation:oldLocation];
// distance 0 ????????
int result = (int)roundf(distance );
if (result < 0) {
result = -result;
}
// long result = (long)distance;
NSLog(gInfo.name );
NSLog(#"Distance i meters: %i", result);
//**************** filter group by check distance *************
if (result <= 10) {
[viewArray addObject:gInfo];
}
Use this
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
latitude = coordinate.latitude;
longitude = coordinate.longitude;
MKCoordinateRegion myRegion;
MKCoordinateSpan mySpan;
mySpan.latitudeDelta = 1.0f;
mySpan.longitudeDelta = 1.0f;
myRegion.center = coordinate;
myRegion.span = mySpan;
//-----------------------------------------------
NSDictionary *dict = (NSDictionary*)[arrayJsonData objectAtIndex:indexPath.row];
CLLocation *locA = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[dict1 valueForKey:#"Latitude"] doubleValue] longitude:[[dict1 valueForKey:#"Longitude"] doubleValue]];
CLLocationDistance distance = [locA distanceFromLocation:locB];
double distanceInMiles = (distance/CONVERSION_TO_MILES);
// CONVERSION_TO_MILES 1609.344

Change from Km to Miles in CLLocationDistance

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]];

Resources