Find the number of routes between two places - objective-c

I need to find out the number of routes from a source to a destination using the Google maps API, and then find, among those, which one is the shortest route.
I am able to get one route by using this code
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:#"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:#"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.google.com/maps?output=dragdir&saddr=%#&daddr=%#", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSLog(#"api url: %#", apiUrl);
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
NSString* encodedPoints = [apiResponse stringByMatching:#"points:\\\"([^\\\"]*)\\\"" capture:1L];
return [self decodePolyLine:[encodedPoints mutableCopy]];
}
but I'm unable to get multiple routes.
I'm new to using the Google Maps API; I followed this tutorial.
How can I do this? Can any one please post some sample code or a tutorial?

alternatives (optional), if set to
true, specifies that the Directions
service may provide more than one
route alternative in the response.
Note that providing route alternatives
may increase the response time from
the server.
From The Google Directions API
You need to add in your query link alternatives=true

For getting the multiple routes you have to use the standard google direction api. Through this api you can get different routes on the bases of traveling mode you select in api ie driving, walking or bicycling( bicycling route availabel in US only) For example : http://maps.googleapis.com/maps/api/directions/xml?origin=srcLatitude,srcLongitude&destination=destLatitude,destLongitude&mode=driving&sensor=false
Here you can give the source and destination latitude and longitude and change the value of mode parameter to driving, walking or bicycling to get multiple route.

Related

How to get Deep Link Data in iOS objective-c

How do you get the data setup in the dashboard.
I have added a key and a value in "Deep Link Data(Advanced)"
In Branch.io dashboard
Login / Menu: Marketing / Actions / Edit
I have this in objective-c:
Branch *branch = [Branch getTestInstance];
[branch setDebug];
NSDictionary *params = [branch getLatestReferringParams];
I am working in "test" of the dashboard.
Alex with Branch here:
The $ symbol is only used to indicate reserved system control parameters (you can find a list of these parameters here). Since you're setting a custom data key, the key : value pair you're using in the example actually just needs to be myplan : 10.
Your session initialization snippet is also a bit odd. Would you mind sharing where you found it? We might have some documentation somewhere that's out of date...
Branch *branch = [Branch getTestInstance];
[branch setDebug];
[branch initSessionWithLaunchOptions:launchOptions andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {
if (!error) {
// params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
// params will be empty if no data found
// ... insert custom logic here ...
NSLog(#"params: %#", params.description);
}
}];

Change Away Status on Nest Thermostat (Nest API)

Using the Nest API I am trying to set the nest thermostat's away status
Reading & Setting for temperature is working fine.
I have the read and write permissions correctly configured for both
thermostat temperature control and for setting thermostat away
I can read the status correctly. Does anyone with some experience of this API know how to go about setting this status?
in "FirebaseManager.h"
Firebase *newFirebase2 = [self.rootFirebase childByAppendingPath:#"structures"];
[newFirebase2 observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
// Put structures into a dictionary
NSMutableDictionary *dict = snapshot.value;
NSLog(#"\n\n\n1. Away Status = %#", [dict valueForKey:#"away"]);
NSLog(#"Dict Contents %#", dict); // <--- Reads thermostat status. A string either home or away
dict[#"away"] = #"away"; //<--- Changes status string but not a correct solution, and does not set the stat to away
//Changes status name but this is not parsed back to firebase
NSLog(#"new status = %#", [dict valueForKey:#"away"]);
}];
To update a child value
assume this structure
structures
structure_id_0
away: "home"
setting the away node to a string of away (this code is quite verbose so it's easy to follow)
Firebase *structuresRef = [self.rootFirebase childByAppendingPath:#"structures"];
//build a reference to where we want to write structures/structure_id/
Firebase *thisStructureRef = [structuresRef childByAppendingPath:#"structure_id_0"];
Firebase *awayRef = [thisStructureRef childByAppendingPath:#"away"];
[awayRef setValue:#"away"];
Now, if you want to do this for snapshots that have been retrieved via observing a node with FEventTypeChildAdded, the node name will be whatever is used in place of structure_id_0. The is the key of the key:value pair.
That can be obtained via snapshot.key.
So NSString *key = snapshot.key
Substitute the key variable in for #"structure_id_0" in the path.
Also check out Firebase Writing Data, and then the updateChildValues for another option.

NMSFTP contentsAtPath returns only a maximum of ~2kB of data

I'm using an SFTP library, https://github.com/Lejdborg/NMSSH, to connect to an SFTP server. Unfortunately when I try and retrieve the contents of a remote file, the data is clipped at about 2kB.
Here's the code I'm using:
NMSSHSession *session = [NMSSHSession connectToHost:host withUsername:username];
BOOL authenticated = [session authenticateByKeyboardInteractiveUsingBlock:^NSString*(NSString *request) {
return password;
}];
NMSFTP *sftpSession = [NMSFTP connectWithSession:session];
...
NSData *data = [sftpSession contentsAtPath:remoteFileName];
When remoteFileName is larger than 2kB, the length of data is always 2000.
Does anyone know why this would be the case?
NB: I've tested this on two different SFTP servers, with the exact same results.
The author of the library fixed the bug (https://github.com/Lejdborg/NMSSH/issues/35)

Using Delta in Dropbox API with iOS

I'm using the dropbox api for iOS and have been messing with the loadDelta function. I get the whole "key" that is sent, and I get how the structure is set up (see below this code), but what I don't understand is how to download the file that is sent and how to save it to the iOS device. Does anyone have any insight on how to do this?
-(void)restClient:(DBRestClient *)client loadedDeltaEntries:(NSArray *)entries reset:(BOOL)shouldReset cursor:(NSString *)cursor hasMore:(BOOL)hasMore{
for (DBDeltaEntry *file in entries) {
if(!file.metadata.isDirectory){
NSLog(#"File: %# ", file.metadata.filename );
}else {
NSLog(#"Directory: %# ", file.metadata.filename );
}
}
}
The call sends back an array called entries. Each entry in entries is this:
#interface DBDeltaEntry : NSObject {
NSString *lowercasePath;
DBMetadata *metadata;
}
with the DBMetadata object being:
#interface DBMetadata : NSObject <NSCoding> {
BOOL thumbnailExists;
long long totalBytes;
NSDate* lastModifiedDate;
NSDate *clientMtime; // file's mtime for display purposes only
NSString* path;
BOOL isDirectory;
NSArray* contents;
NSString* hash;
NSString* humanReadableSize;
NSString* root;
NSString* icon;
NSString* rev;
long long revision; // Deprecated; will be removed in version 2. Use rev whenever possible
BOOL isDeleted;
NSString *filename;
}
What I can't figure out is how to recursively set up my offline structure or the best practice for doing so. My assumption is though, using delta, I won't need to keep a database of the files I have saved for update purposes, right?
The /delta call only tells you what has changed. It doesn't itself give you any access to the file contents. If you want to download any particular file that you heard about from /delta, you should use the path it gave you with the /files (GET) call to download the file:
https://www.dropbox.com/developers/reference/api#files-GET
(The iOS SDK makes this available as the loadFile function.)
The /delta call does save you the trouble of having to call /metadata to manually figure out what has changed and keep track of the current state, but note that the Dropbox API best practices ( https://www.dropbox.com/developers/reference/bestpractice ) do say that you shouldn't download anything until the user asks for it.
One scheme is to use a SQLite table with the following columns:
lc_path: The lowercase'd path of the file (primary key for this table)
name: The name of the file
lc_parent_path: The lowercase'd path of the parent folder
other metadata... (last modified, revision, is_dir, etc.)
So when processing an "add" /delta entry, you insert a row into the table (you might have to replace an existing row).
When processing a "delete" entry for the path /a/b/c, you need to delete all children as well, so you can do DELETE ... WHERE lc_path = "/a/b/c" and then DELETE ... WHERE lc_path LIKE "/a/b/c/%".
If you want to query the database for a list of immediate children of the folder "/a/b/c", do SELECT ... WHERE lc_parent_path = "/a/b/c"
You may have noticed that you the path is stored somewhat redundantly (lc_parent_path+name and lc_path). This probably won't be a problem. But if you find that your database is too large and that most of the space is going toward storing the path strings, there are encoding tricks you can do.

How to parse this

I'm experimenting with googles url shortener goo.gl and at the moment it send the url to goo.gl and returns this:
{
"kind": "urlshortener#url",
"id": "http://goo.gl/kyPI",
"longUrl": "http://dsfsd.com/"
}
I think it's JSON but I'm having trouble with that because of ARC. Is there anyway else I could parse the string with key "id"?
If your app targets iOS 5 you can use NSJSONSeralization class of Apple.
(Let's assume youryour received is called theData)
NSError *error=nil;
id result=[NSJSONSerialization JSONObjectWithData:theData options:
NSJSONReadingMutableContainers error:&error];
//to retrieve the 'kind' value of the object
NSLog("Kind: %#",[result objectForKey:#"kind"]);
It's JSON, you can use a framework like SBJson to parse it. See http://stig.github.com/json-framework/