decode google maps PolyLines objective C

If you are working on Google maps API and using routes/directions
this will help you decode route polylines
works on MAC OS X and iPhone SDK

-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[polyLines addObject:loc];
NSLog(@"%f,%f",[latitude floatValue],[longitude floatValue]);
[loc release];
[latitude release];
[longitude release];
}
return array;
}

About Hasnat

I R TEH 1337
This entry was posted in Other. Bookmark the permalink.

4 Responses to decode google maps PolyLines objective C

  1. kosso says:

    hi hasnat,

    sorry for commenting on this post, but I needed to contact you – regarding a comment you made on a post over at trailsinthesand.com/exploring-iphone-audio-part-7

    Firstly, thanks so much for the lines of code regarding the buttons! very helpful indeed ;)

    You seem to say that you managed to get that code working.

    I’ve managed to get the audio recording app to build now with no warnings or errors.

    I changed the printfs to NSLog outputs and also had to change fsRdPerm to 0×01 in AudioFileOpenURL.

    The app installs fine, but on launching it gets to the ‘init’ (where added an NSLog debug message) and then it just closes with no more debug info. :/ hmmm…

    I’m building against SDK 3.1.2

    Any idea what I might be doing wrong?

    I did add the AudioToolbox framework to a new ‘Window-based application’ project too.

    Any pointers would be most, most appreciated. Thanks man! ;)

    Kosso

  2. nt says:

    there seems to be some kind of error in the decoding.it works for small distances, but try something bigger, like directions from Madrid to Athens and see the resulting polyline…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>