Get part of the map view as an image in iOS
I am creating an app where in I have to show just the part of the area where on my place will be plotted, Similar like the one below.
Clicking on this will image will take my app further. But here it开发者_JAVA技巧's just an static image generated depending upon my longitude and latitude.
Any help would be really appreciated. Thanks
Here's the working code. Incase anybody's still searching out for the answer.
NSString *staticMapUrl = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?markers=color:red|%f,%f&%@&sensor=true",yourLatitude, yourLongitude,@"zoom=10&size=270x70"];
NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:mapUrl]];
Thanks
You can try Google Static Maps API
Here's sample code
NSString *urlString = @"http://maps.googleapis.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=200x200&sensor=false";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *imageData = [UIImage imageWithData:data];
[yourImageView setImage:imageData];
Through this you can get static image based on your coordinates. But I'm afraid to say that customization of annotation may not be possible.
You could also use MKMapSnapshotter
, this allows you to take a screen shot of the map;
MKMapSnapshotOptions * snapOptions= [[MKMapSnapshotOptions alloc] init];
self.options=snapOptions;
CLLocation * Location = [[CLLocation alloc] initWithLatitude:self.lat longitude:self.long];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(Location.coordinate, 300, 300);
self.options.region = region;
self.options.size = self.view.frame.size;
self.options.scale = [[UIScreen mainScreen] scale];
MKMapSnapshotter * mapSnapShot = [[MKMapSnapshotter alloc] initWithOptions:self.options];
[mapSnapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (error) {
NSLog(@"[Error] %@", error);
return;
}
UIImage *image = snapshot.image;//map image
NSData *data = UIImagePNGRepresentation(image);
}];
What you are wanting to do is fundamental mapkit api functionality. Check out the documentation here:
http://code.google.com/apis/maps/articles/tutorial-iphone.html
and one of my favorite ios blogs has a mapkit tutorial here:
Today there is this, without Google: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/MapKit/MapKit.html Find the code at paragraph "Creating a Snapshot of a Map". So the result looks consistent with Apple Maps.
I would like to add to @DevC answer, by providing a method (with completion block) that adds an annotation in the middle of the image. At first I was interested in seeing if I could add an annotation before the snapshot begins, however MKMapSnapshotter
does not support this. So the solution I came up with, is to create a pin, draw it on top of the map image and then create another image of the map + pin. Here is my custom method:
-(void)create_map_screenshot:(MKCoordinateRegion)region :(map_screenshot_completion)map_block {
// Set the map snapshot properties.
MKMapSnapshotOptions *snap_options = [[MKMapSnapshotOptions alloc] init];
snap_options.region = region;
snap_options.size = // Set the frame size e.g.: custom_view.frame.size;
snap_options.scale = [[UIScreen mainScreen] scale];
// Initialise the map snapshot camera.
MKMapSnapshotter *map_camera = [[MKMapSnapshotter alloc] initWithOptions:snap_options];
// Take a picture of the map.
[map_camera startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
// Check if the map image was created.
if ((error == nil) && (snapshot.image != nil)) {
// Create the pin image view.
MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil];
// Get the map image data.
UIImage *image = snapshot.image;
// Create a map + location pin image.
UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale); {
[image drawAtPoint:CGPointMake(0.0f, 0.0f)];
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
// Create the pin co-ordinate point.
CGPoint point = [snapshot pointForCoordinate:region.center];
if (CGRectContainsPoint(rect, point)) {
// Draw the pin in the middle of the map.
point.x = (point.x + pin.centerOffset.x - (pin.bounds.size.width / 2.0f));
point.y = (point.y + pin.centerOffset.y - (pin.bounds.size.height / 2.0f));
[pin.image drawAtPoint:point];
}
}
// Get the new map + pin image.
UIImage *map_plus_pin = UIGraphicsGetImageFromCurrentImageContext();
// Return the new image data.
UIGraphicsEndImageContext();
map_block(map_plus_pin);
}
else {
map_block(nil);
}
}];
}
This method also requires a completion header to be declared like so:
typedef void(^map_screenshot_completion)(UIImage *);
I hope this is of some use to people looking to add annotations to the map image.
To be clear, there is no way to get an image for a specific location through map kit.
You have to use the MKMapView, disable user from panning/zooming, add annotation and handle click on annotation to do whatever you wanted to do..
精彩评论