Finding distance between CLLocationCoordinate2D points
You should create an object of CLLocation using,
- (id)initWithLatitude:(CLLocationDegrees)latitude
longitude:(CLLocationDegrees)longitude;
Then, you should be able to calculate the distance using
[location1 distanceFromLocation:location2];
Distance between 2 points with CLLocationCoordinate2D
Use the below method for finding distance between two locations
-(float)kilometersfromPlace:(CLLocationCoordinate2D)from andToPlace:(CLLocationCoordinate2D)to {
CLLocation *userloc = [[CLLocation alloc]initWithLatitude:from.latitude longitude:from.longitude];
CLLocation *dest = [[CLLocation alloc]initWithLatitude:to.latitude longitude:to.longitude];
CLLocationDistance dist = [userloc distanceFromLocation:dest]/1000;
//NSLog(@"%f",dist);
NSString *distance = [NSString stringWithFormat:@"%f",dist];
return [distance floatValue];
}
How to find out distance between coordinates?
CLLocation has a distanceFromLocation method so given two CLLocations:
CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];
or in Swift 4:
//: Playground - noun: a place where people can play
import CoreLocation
let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)
let coordinate₁ = CLLocation(latitude: 5.0, longitude: 3.0)
let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters
you get here distance in meter so 1 miles = 1609 meter
if(distanceInMeters <= 1609)
{
// under 1 mile
}
else
{
// out of 1 mile
}
CLLocationCoordinate2D distance between two points considering zoom level
You can use convertCoordinate:toPointToView:
to get the location of the actual screen point for an annotation:
CGPoint annotationPoint = [self.mapView convertCoordinate:annotation.coordinate
toPointToView:self.mapView];
After that, use your trigonometry skills to find the distance between two points.
Calculating Distance between two coordinates using CLLocation
The error you're getting is actually:
Initializing 'CLLocationDistance *' (aka 'double *') with an expression of incompatible type 'CLLocationDistance' (aka 'double')
What it's saying is you're initializing itemDist
(which you've declared as a CLLocationDistance *
) to something that is returning a CLLocationDistance
(notice no asterisk).
CLLocationDistance
is not an object.
It is just a primitive type (specifically double
-- see the Core Location Data Types Reference).
So instead of declaring itemDist
as a pointer to a CLLocationDistance
, just declare it as a CLLocationDistance
(no asterisk):
CLLocationDistance itemDist = [itemLoc distanceFromLocation:current];
You'll also need to update the NSLog
to expect a double instead of an object otherwise it will crash at run-time:
NSLog(@"Distance: %f", itemDist);
distanceFromLocation - Calculate distance between two points
Try this instead:
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
The method you're trying to use is a method on a CLLocation object :)
Finding the distance between current location and another location (Swift 5)
coordinate
is of type CLLocationCoordinate2D
You only need locationManager.location
if let location = locationManager.location {
print(phx.distance(from: location))
}
Calculate offset between two CLLocationCoordinate2D locations
If I understand, what you're trying to get is the distance between 2 known location in degrees ?
If this is it then try :
class func getDistancesInDegrees(origin:CLLocationCoordinate2D, destination:CLLocationCoordinate2D) -> (degLat: CLLocationDegrees, degLon:CLLocationDegrees) {
var latidueDegrees:CLLocationDegrees = Double(origin.coordinate.latitude) - Double(destination.coordinate.latitude)
var longitudeDegrees:CLLocationDegrees = Double(origin.coordinate.longitude) - Double(destination.coordinate.longitude)
return (degLat: latidueDegrees, degLon:longitudeDegrees)
}
How to find the distance form a CLLocationCoordinate2D to a GMSPath?
You need to use cordinate+ trigonometry geometry.
If you know the start and end points of the path, you can draw an imaginary line between this points and find a point which is perpendicular from your CLLocationCoordinate2D to this imaginary line.
If your start location is called CLLocationCoordinate2D start, and your end location is CLLocationCoordinate2D end, you first need to find the slope of the equation for this line. This would be ABS(start.latitude - end.latitude) converted into meters and similarly ABS(start.longitude - end.longitude) converted into meters.
Then your slope = (height/width)
Your equation for your imaginary line is y = slope * x - where your imaginary origin is your start location.
Have a look at this
Related Topics
Load a .Tmx (Tiled Map) in Sprite Kit
Swift Put Multiple Iboutlets in an Array
Send Image Along with Other Parameters with Afnetworking
How to Sort Nsmutablearray of Date Objects
Sequence Animation Using Caanimationgroup
How to Implement Tableview Inside Tableview Cell in Swift 3
Get Each Line of Text in a Uilabel
How to Truncate Decimals to X Places in Swift
Enforce Collectionview to Have Only 2 Rows
Multiple Unusernotifications Not Firing
Horizontally Scroll All Rows of Uicollectionview Together