Showing a location with respect to another location without using Map - ios

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

Related

Calculate distance between latitude and longitude using MapKit

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

how to calculate distance between two points using skobbler maps in objective c

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

Google Maps SDK for iOS How to Identify if a location is within 5 miles of current location?

I have a list of lat/longs that represent the places I want to show on the Google Maps.
How can I check which of these places are near my current location. Something like Near Me functionality that we generally see.
I am using Google Maps SDK for iOS.
Hope the question is clear now?
Make sure that you have imported CoreLocation Framework
CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
CLLocationDistance distance = [locA distanceFromLocation:locB];
//Distance in Meters

Map view span changes when updating the location

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

iOS: distance of route between two locations (route, not straight line)

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.

Resources