Am trying to calculate distance from one point to another. I am able to find method for that.
CLLocationDistance dist = [loc distanceFromLocation:loc2];
But what it does is calculate distance in a straight line.
What I want is to calculate distance by roadways.
For Example: Lets say If i give from address as Home and To address as Work. I want the distance between Home to Work by roadways. By roadways I get 750meters. But if i calculate by the method distanceFromLocation I get only 400meters.
Can someone help me with an example to calculate distance.
Check out this answer :
CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate];
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];
CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];
float distanceMeters = [pointALocation distanceFromLocation:pointBLocation];
float distanceMiles = (distanceMeters / 1609.344);
Here pointAAnnotation & pointBAnnotation are the two Annotations which you need to calculate distance between them.
Ref : Distance bw two Latitude & Longitude
Related
I wanted to find the distance between the origin lat long and destination lat long of the Polyline i have drawn using google api.
Form CLLocation object like
CLLocation *sourceLocation = [[CLLocation alloc]initWithLatitude:sourceCoordinate.latitude longitude:sourceCoordinate.longitude];
CLLocation *destinationLocation = [[CLLocation alloc]initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
and pass them to this method.
-(CLLocationDistance)getDistanceFromSource:(CLLocation*)source andDestination:(CLLocation*)destination {
CLLocationDistance distance = [source distanceFromLocation:destination];
return distance;
}
In google maps we can use distanceTo calculate distance
I dont know how to do in skobbler maps
- (void)positionerService:(SKPositionerService *)positionerService updatedCurrentLocation:(CLLocation *)currentLocation;
If my memory doesn't go wrong, there is a property currentCoordinate for positionService. Therefore, distance can be calculated like this:
CLLocation *skLocation = [[CLLocation alloc] initWithLatitude:positionService.currentCoordinate.latitude longitude:positionService.currentCoordinate.longitude];
CLLocationDistance distance = [currentLocation distanceFromLocation:skLocation];
Below is the code snippet I use to convert pixel to coordinates and vice versa.
CLLocationCoordinate2D currentLocation = CLLocationCoordinate2DMake(geoPoint.latitude, geoPoint.longitude);
[mapView setCenterCoordinate:currentLocation];
CGPoint centerScreenPoint = [mapView coordinateToPixel:mapView.centerCoordinate];
CLLocationCoordinate2D loc = [mapView pixelToCoordinate:centerScreenPoint];
geoPoint on the first line is the current location reported by CLLocationManager. Although centerScreenPoint gives me 160, 284 which is the center point on iPhone 5, after running this code and dumping the values of currentLocation and loc I get two different coordinates.
How do these points come out different, any ideas?
I am creating a location based iPhone App.
I have two locations (their lat and long).
One location is fixed to center of the UIView, How can show the other location with respect to first location.
Please give me code if available.
If you're just trying to find the distance between your two points then you could use the CoreLocation function:
CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:startLatitude longitude:startLongitude];
CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:endLatitude longitude:endLongitude];
CLLocationDistance distanceMeters = [startLocation distanceFromLocation:endLocation];
I have a MKMapview with multiple annotations. When the user chooses an annotation, the "callout" view shows the location's name and the distance between the location and the user location.
I'm stuck at getting the distance. Prior to iOS 5, I could use Google direction API to get the distance, but now I can't. Is there any alternative solution?
Thank you!
EDIT:
The distance I refer is the distance of the route between two locations. The route has many routes. It's not the distance of the straight line as the result of "distanceFromLocation:"
If you have two CLLocations, you can use the distanceFromLocation: method to get the distance in meters between them.
Example:
CLLocation* first = [[CLLocation alloc] initWithLatitude:firstLatitude longitude:firstLongitude];
CLLocation* second = [[CLLocation alloc] initWithLatitude:secondLatitude longitude:secondLongitude];
CGFloat distance = [first distanceFromLocation:second];
You can get the latitude and longitude of your annotation (not your annotation view) from its coordinate or similar property on your annotation class.
If you have a list of annotations and want to get the distance of a path between them, simply:
CGFloat distance = 0;
for(int idx = 0; idx < [mapView.annotations count] - 1; ++idx) {
CLLocationCoordinate2D firstCoord = [mapView.annotations[idx] coordinate];
CLLocationCoordinate2D secondCoord = [mapView.annotations[idx + 1] coordinate];
CLLocation* first = [[CLLocation alloc] initWithLatitude:firstCoord.latitude longitude:secondCoord.latitude];
CLLocation* second = [[CLLocation alloc] initWithLatitude:secondCoord.latitude longitude:secondCoord.longitude];
distance += [first distanceFromLocation:second];
}
Now (iOS 7 and later) you can get a route with MKDirections, MKDirectionsRequest and MKDirectionsResponse.
The route information is MKRoute (there's a distance property).
For an example follow this link.