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.
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;
}
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
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'm using the following code to calculate the distance between my app user's location, and nearby locations (annotations). When I try to display the "Distance Away" as text in a label, it keeps saying that the calculated distance is 0. Why is this?
*Note that my table is using the coordinates placed on my MapView to determine the distance away.
Here's the code I'm using to calculate the distance from my user to a nearby location
MapViewController.m
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
for (MapViewAnnotation *annotation in self.mapView.annotations) {
CLLocationCoordinate2D coord = [annotation coordinate];
CLLocation *annotationLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
annotation.distance = [newLocation distanceFromLocation:annotationLocation];
}
And here's the bit I'm using to display the calculatedDistance in a label (located in my custom cell to be displayed in a tableview):
ViewController.m
cell.distanceLabel.text = [NSString stringWithFormat:#"%f m.", calculatedDistance];
Any help would be much appreciated!
for (MapViewAnnotation *annotation in self.mapView.annotations) {
CLLocationCoordinate2D coord = [annotation coordinate];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
That's not the user's location, that's the annotation's location. But odd variable names aside:
CLLocationDistance calculatedDistance = [userLocation distanceFromLocation:userLocation];
The distance from a location to itself is 0. That line is probably a bug. Maybe you meant
CLLocationDistance calculatedDistance = [newLocation distanceFromLocation:userLocation];
which would return the distance between the user's new location and the (misnamed) annotation's location - probably what you want. Of course, you actually calculate just that on the line above the distance-to-self calculation, then throw it away two lines later.
tl;dr
Throw away the last two lines, they're useless.
I have a function in my view controller with a mapkit that is called when the location changes. I would like set to the map view so that is is centered around the current location and moves with it as it updates.
It works in a sense i.e it tracks along, however it is always zoomed in really far. If I zoom out it snaps back to where it was when it calls the update again after getting a new location.
in from params CLLocation *loc
bool first = TRUE;
if(first){
first=FALSE; // bit of a bodge...if i set this to false it will track the update possition but not at the desired "zoom" its look far to close.
CLLocation *loc = [[CLLocation alloc] initWithLatitude:54.976619 longitude:-1.613118];//newcastle city center.
CLLocationDegrees spanLat = 1000;
CLLocationDegrees spanLng = 1000;
userLocation = MKCoordinateRegionMakeWithDistance(loc.coordinate, spanLat, spanLng);
[mapview setRegion:userLocation animated:TRUE];
}else {
CLLocationDegrees spanLat = [mapview region].span.latitudeDelta;// keep the originals? DOESN'T WORK!!
CLLocationDegrees spanLng = [mapview region].span.longitudeDelta;//
userLocation = MKCoordinateRegionMakeWithDistance(loc.coordinate, spanLat, spanLng);
[mapview setRegion:userLocation animated:TRUE];
}
Just keep setting the center coordinate and don't set the region.
By the way, you do know that you don't need any of that code, right? You can just tell the mapview to track the device's current location. See my book for an example:
http://www.apeth.com/iOSBook/ch34.html#_map_kit_and_current_location