Get direction of User by CLLocation - iphone

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

Related

Can I change my location on the device to test a user's location?

I'm using the following code to get the current lat loc of the user. I would like to change the currentLocation of the user on the device to see how the map is populated with annotations. I would like to emulate the problem a tester of the app is having on the device in another country than I'm in. For him the map doesn't load the annotation whereas for me it works fine in the Simulator and the device (we're using the same device model etc.). I did try setting the currentUserLatitude currentUserLongitude to my tester's values but the map added the annotations fine for me.
-(void)loadMap{
CLLocationCoordinate2D coordinate = [self getLocation];
currentUserLatitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
currentUserLongitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"*dLatitude : %#", currentUserLatitude);
NSLog(#"*dLongitude : %#",currentUserLongitude);
}
-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
You can't simulate a location on a physical device.
However, it is possible to test your app in the simulator and have it claim to be in whatever location you would like.
Yes. You can change your device location. Run your project and go to Xcode Debug Area Click on Simulate Location and select the location from list.Before that you need to allow simulate location from edit scheme
[![enter image description here][1]][1]
You can set coordinates manually
-(CLLocationCoordinate2D) getLocation{
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(47.1726, 9.5153);
return coordinate;
}
Or use simulator.

Get coordinates by giving name of place in iphone

Is it possible to get coordinates by giving name of a place in iPhone? I don't want to use any Web Service as they have few limitations. Can i do it using iPhone SDK?
Thanks in advance.
As #raj2raaz have mentioned, we can'nt get coordinates by just specify the name of the place, you must have to use web servics in iPhone to get coordinates.
http://iphonesdksnippets.com/post/2010/02/15/Get-Coordinates-from-Address.aspx
The short answer is no, you can't give an address and get the longitude / latitude position. Several people have written libraries that use the different web services, see this answer for details: Forward geocoding from the iPhone .
I know that you said you don't want to use various web services, but, well, you can't get everything for free. Somebody's CPU cycles are going to have to do a search. Most of them seem to me to have terms that are acceptable for most applications.
Yes, it's possible, but you have to do some work. I am currently putting this effort in one of my project. The GeoNames geographical database covers all countries and contains over eight million placenames that are available for download free of charge. I am adding their cites with population over 1,000 3.9M zip file to my project. It's contain long/lat of each city. I am going to parse each city into a custom NSObject and load them into Core Data.
In my project, instead of doing a city name search, I am going to find the closet city to a particular lat/long coordinate. Haversine formula is used to calculate the distance between two points on a sphere. Here are the formula written in Objective-C and Perl.
My current progress of parsing that data can be found here. I still have work to complete.
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:YOUR_LOCATION completionHandler:^(NSArray* placemarks, NSError* error)
{
NSLog(#"completed");
if ( error )
{
NSLog(#"error = %#", error );
}
else
{
//Here you get information about the place in placemarks array
}
}];
you can get the coordinates by calling this method .this method requires the address(name of place).
-(CLLocationCoordinate2D) addressLocation:(NSString *)addrss {
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[addrss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}

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.

Calculating driving distance in iPhone

I need to find the driving distance between 2 locations. I do not need to display the directions in a map, just need to calculate the distance, which I need to use in my application.
Does MapKit allow this? Is there an alternative that can be used?
I am able to get forward geo-coding using CloudMade, but there doesn't seem to be an option to obtain driving distance.
Appreciate any help.
CloudMade also offers driving directions. If you are only interested in the distance, simply ignore the instructions.
An API-Call looks like this:
http://navigation.cloudmade.com/YOUR-API-KEY-GOES-HERE/api/0.3/47.25976,9.58423,47.26117,9.59882/car/shortest.js
and the JSON includes
....
route_summary: {
total_distance: Distance in meters,...
Source
Found this in Google Groups, is that helpful?
http://groups.google.com/group/google-maps-api/msg/4dc2fad4f74e3314?pli=1
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.