Calculating driving distance in iPhone - 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.

Related

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

iOS Location based reminder

i am making an application which has a feature that allows users to create location based notifications to turn the application on/off when they arrive/leave a certain location.
Reminders are created (as indicated by the first picture), but are not triggered upon arriving/leaving.
If on the other hand the user click on the reminder, it kind of adds the address (shown on picture number 2) and is from there on triggered
I was wondering if there is a way to make the Reminder app recognize the address or any other suggestion, that might help me in solving this peculiar problem.
Thank you in advance,
BR,
Rok
The code that i use is:
EKReminder *reminder = [EKReminder reminderWithEventStore:_eventStore];
reminder.calendar = [_eventStore defaultCalendarForNewReminders];
EKStructuredLocation *location;
NSError *error = nil;
EKAlarm *alarm = [[EKAlarm alloc]init];
reminder.title = #"Turn off Test App";
location = [EKStructuredLocation locationWithTitle:self.addressTextField.text];
[self.addressTextField resignFirstResponder];
alarm.proximity = EKAlarmProximityEnter;
alarm.structuredLocation = location;
[reminder addAlarm:alarm];
[_eventStore saveReminder:reminder commit:YES error:&error];
The problem is that you are failing to set your EKStructuredLocation's geolocation and radius. All it has is a title. That isn't enough to tell the alarm where on earth it is supposed to be!
Example:
location.geoLocation =
[[CLLocation alloc] initWithLatitude:latit longitude:longit];
location.radius = 10*1000; // metres
See CLLocationManager -startMonitoringForRegion:, and the CLRegion class reference.

MapKit in iOS 6 - How to Find Places Nearby...?

Using MapKit in iOS 6, how am I'm supposed to get nearby locations without having their specific coordinates? I'm also unsure if it's still possible...err...allowed...to use Google Maps API to accomplish this goal, as this is the only way I can think of to do this. I know everything is still in beta, but I've still found no information anywhere about this topic, on forums, in Apple's new MapKit Documentation, anywhere. All I want to do is perform a search for locations (let's say, parks, for example) within 'x' miles of the user's location.
It seems that since Apple has developed their own Maps application, they should have a way to accomplish this using MapKit or Core Location...right?
Try with this code. This may help to you.
URLManager *urlmanager = [[URLManager alloc] init];
urlmanager.delegate = self;
urlmanager.responseType = JSON_TYPE;
urlmanager.commandName = #"Search";
NSString *locationString = [NSString stringWithFormat:#"%f,%f",latitude,longitude];
//Location where you want to search
NSString *key = #"AIzaSyCNRpero6aM451X0IfgFHAd-Y3eJUssqoa8`enter code here`0E";
//This is the secret key which you will get from the Google API Console when you register your app.
NSString *radiuos = #"15000";
//This is the area within which you want to search
NSString *keyword = #"Hotel";//Search keyword you want to search
NSMutableDictionary *arguments = [[NSMutableDictionary alloc] init]; // zero argument
[arguments setValue:key forKey:#"key"];
[arguments setValue:locationString forKey:#"location"];
[arguments setValue:radiuos forKey:#"radius"];
[arguments setValue:#"true" forKey:#"sensor"];
[arguments setValue:keyword forKey:#"keyword"];
NSLog(#"Arguments are %#",arguments);
[urlmanager urlCallGetMethod:[NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json"] withParameters:arguments];

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

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.