29
Jan
10

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

FKN Share and Enjoy:
  • blogmarks
  • del.icio.us
  • Furl
  • Reddit
  • Digg
  • Facebook
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati
  • TwitThis
  • Google

3 Responses to “decode google maps PolyLines objective C”


  1. 1 kosso Feb 2nd, 2010 at 4:26 am

    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. 2 EMiN3M Feb 2nd, 2010 at 7:51 am
  3. 3 kosso Feb 2nd, 2010 at 5:00 pm

    u fkn r00l!

Leave a Reply




March 2010
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
293031  

Categories

No categories

Xbox360

PS3

Hasnat's PSN

Flickr

www.flickr.com