Skip to content

Instantly share code, notes, and snippets.

@benjaminpearson
Created November 28, 2012 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminpearson/4159123 to your computer and use it in GitHub Desktop.
Save benjaminpearson/4159123 to your computer and use it in GitHub Desktop.
Inlight Media - Blog - Using the iOS MapKit framework
UIImage *annotationImage = [UIImage imageNamed:@"annotation-image"]; //NOTE: Using a UIImageView will not work
annotationView.image = annotationImage; // NOTE: Make sure annotationView is an instance of MKAnnotationView, not MKPinAnnotation
double zoomLevel = [mapView getZoomLevel]; // getZoomLevel is implemented in the category
if(zoomLevel > 16) {
[mapView setCenterCoordinate:[mapView centerCoordinate] zoomLevel:16 animated:YES];
}
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 60), NO, 2); //Set scale factor to 2 to cater for @2x images
[pinBackground drawInRect:CGRectMake(0,0,40,60)];
[pinForeground drawInRect:CGRectMake(4,4,32,32)];
UIImage *pinImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *url = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
(NSArray *)decodePolyLine:(NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
[encoded replaceOccurrencesOfString:@"points:\"" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *listCoordinates = [[NSMutableArray alloc] init];
NSInteger lat=0;
NSInteger lng=0;
while (index < len - 1) {
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]];
[listCoordinates addObject:loc];
}
return [NSArray arrayWithArray:listCoordinates];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment