Short dictanse from given latitude and longitude iphone - iphone

In my application i have array of latitude and longitude with 5-6 places i want to short distance from my current location and show it on my map view for example: i have 3 places location A,B and C so i want to short them according to my current location if location C is near to my current location then first i want to show C location on MAP after that calculate location A and B with location C means which location is near to place C if B is near to place C then show B location on map and at the last show location A
How can we do that ?

To see the distance between two locations
CLLocation *locA = [[CLLocation alloc] initWithLatitude:latA longitude:longA];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:latB longitude:longB];
CLLocation *locC = [[CLLocation alloc] initWithLatitude:latC longitude:longC];
CLLocation *yourLoc = [[CLLocation alloc] initWithLatitude:yourLat longitude:yourLong];
then get the distances between the place and your location
CLLocationDistance distance = [locA distanceFromLocation:yourLoc];
CLLocationDistance distance2 = [locB distanceFromLocation:yourLoc];
CLLocationDistance distance3 = [locC distanceFromLocation:yourLoc];
and finally compare the distances.

Related

location monitoring in a iphone app

I am an newbie with iphone app development. I would like to capture location information without using GPS but only with Cell tower info and Wifi data. To enable this I am using the CLLocationManager to capture location information,
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringSignificantLocationChanges];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
float latitude = newLocation.coordinate.latitude;
float longitude = newLocation.coordinate.longitude;
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
// NSTimeInterval is defined as double
NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
NSLog(#"latitude %f and longitude %f", latitude, longitude );
NSLog(#"%d", [timeStampObj integerValue]);
NSMutableArray *numArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithDouble:latitude],[NSNumber numberWithDouble:longitude], timeStampObj, nil];
}
When I use the above code when the app is launched I see that the app requests for accessing location services. Once this is accepted, I am not sure if the app is making use of GPS to get the location information as I see a small arrow pointing out, which seems as though GPS is being used to obtain the location information, which is not what I require. I would want the location information to be captured using WiFi and Cell Tower information only.
Also is there a frequency in which this location information gets updated, as there is an initial update of location after that I do not see the location getting modified even after the phone is being continuously moved.
If you use this:
[locationManager startMonitoringSignificantLocationChanges];
You are only getting GPS uploads when a significant change happens (for example, when you move from a telephony tower to other).
You cannot choose if you want to use GPS or towers from the code.

Get direction of User by CLLocation

Hi, i am working on GPS ios app.
I want to display user's location on Circle as shown in image.
The user is in center.
I want to display his friend's location on proper direction.
I had already got langitude and latitude of both user .
But how can i get other user's direction?
If can i use Bearing angle?
I get the Distance between two uses by this code
CLLocation *locA = [[CLLocation alloc] initWithLatitude:Latitude longitude:Longitude];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:app.lat longitude:app.lag];
CLLocationDistance distance = [locB distanceFromLocation:locA];
Thanks for Help!!
You can do it using two ways :
1.
CLLocationCoordinate2D start = { c_lat, c_long };
CLLocationCoordinate2D destination = { [[dic valueForKey:#"business_lat"]doubleValue], [[dic valueForKey:#"business_long"]doubleValue] };
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f&",start.latitude, start.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
2.
Using Google API
In API you just need to pass origin name and destination name
Hope this helped....
This question describing how to compute bearing using the haversine function

CLLocationCoordinate2D invalid initializer

I am using CLLocationCoordinate2D in to assign latitude and longitude like this
CLLocationDegrees latitude = [somelat doubleValue];
CLLocationDegrees longitude = [somelongi doubleValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
When i run this code in iPhone simulator its not showing error but wheni run the same application in ios simulator 3.2 that is the iPad simulator its showing me an "Invalid Initializer" error.
How can i resolve this...Please anybody suggest me a solution to this problem
Thanks and regards,
Sankar Chandra Bose
CLLocationCoordinate2DMake is available only on iOS 4.0 and later. Do this rather,
CLLocationCoordinate2D coordinate;
coordinate.longitude = longitude;
coordinate.latitude = latitude;
CLLocationCoordinate2DMake is available only from ios 4.0. Check the reference
CLLocationCoordinate2DMake
Formats a latitude and longitude value
into a coordinate data structure
format.
CLLocationCoordinate2D
> CLLocationCoordinate2DMake(
> CLLocationDegrees latitude,
> CLLocationDegrees longitude)
Parameters
latitude
The latitude for the new coordinate. longitude
The longitude for the new coordinate.
Return Value
A coordinate structure encompassing
the latitude and longitude values.
Availability
Available in iOS 4.0 and later.

convert nsstring to cllcoordinate

i am working on a application where i am saving the coordinates of a location in database
now i need to use these coordinates and display the location on a map. Can some one please
suggest me how can i change the latitude & longitude stored as NSString to coordinate. I want
to use these coordinates to display an anotation on the map.
Thanks in advance
CLLocationCoordinate2D anyLocation = [[CLLocationCoordinate2D alloc] init];
anyLocation.latitude = [latText doubleValue];
anyLocation.longitude = [lonText doubleValue];
latText and lonText are strings

Use mapkit to calculate driving distance between two addresses?

Is it possible to use the mapkit in the iphone sdk to calculate driving distance between two addresses ?
How about using the Core Location framework:
- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location
You'll need the latitude/longitudes for both addresses.
Edit: Deprecated in iOS 3.2. Use the distanceFromLocation: method instead.
With iOS7 you get full directions within the API
Take a look at this tutorial Tutorial.
No but if you have the longitude/latitude then it is fairly easy to calculate the distance. It is the mathematical distance between those points of course. Not the actual driving or walking distance that is based on an actual route.
In my applications I used MKDirections to get driving (walking) distance between two location.
CLLocationCoordinate2D startCoordinates = YOUR_START_COORDINATES;
CLLocationCoordinate2D endCoordinates = YOUR_END_COORDINATES;
MKPlacemark *startPoint = [[MKPlacemark alloc] initWithCoordinate:startCoordinates];
MKPlacemark *endPoint = [[MKPlacemark alloc] initWithCoordinate:endCoordinates];
MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startPoint];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endPoint];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = startItem;
request.destination = endItem;
request.transportType = MKDirectionsTransportTypeAutomobile; //here you can choose a transport type you need
MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (response) {
for (MKRoute *route in response.routes) {
NSLog(#"Distance : %f", route.distance);
}
}
}];
if you have you location as an address, so you can use the method of CLGeocoder, which will give you latitude and longitude of your address
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
If you compare a driving distance result using MKDirections with the one using Google Maps, you will see that they differ. I was searching regarding this matter and came across the following link http://www.macworld.co.uk/review/reference-education/apple-maps-vs-google-maps-3464377/
Even though Apple have been improving their map service, they still concede in accuracy to Google (IMO), at least in a question regarding the driving distance.
So if accuracy is not highly important in your case, so you can follow along with Apple. Otherwise I would recommend checking Google API.