在iOS中将地图视图的一部分作为图像获取

7
我正在创建一个应用程序,需要显示我的位置的一部分地区的区域图像,类似下面的图像。单击此图像将使我的应用程序进一步运行。但这只是一个静态图像,根据我的经度和纬度生成。希望能得到任何帮助。谢谢。
7个回答

35

以下是正常运行的代码,如果还有人在寻找答案。

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

谢谢


3
您可以尝试使用Google静态地图API
以下是示例代码。
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];

通过这个功能,您可以基于您的坐标获取静态图像。 但是很遗憾地告诉你,注释的自定义可能不可行。


3
您还可以使用MKMapSnapshotter,它允许您对地图进行截屏;
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);

        }];

类型对象上找不到属性“options”。 - Shahbaz Akram
@ShahbazAkram,你应该声明一个名为options的MKMapSnapShotOptions属性。 - DevC

1

0

我想补充一下@DevC的答案,提供一个带有完成块的方法,可以在图像中间添加注释。起初,我想看看是否可以在快照开始之前添加注释,但是MKMapSnapshotter不支持此功能(链接)。因此,我想出的解决方案是创建一个标记,在地图图像顶部绘制它,然后创建另一个包含地图和标记的图像。这是我的自定义方法:

-(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);
        }
    }];
}

这个方法还需要声明一个完成头,像这样:

typedef void(^map_screenshot_completion)(UIImage *);

我希望这对于想要向地图图像添加注释的人们有所帮助。


0

-2
要明确的是,通过地图工具包无法获取特定位置的图像。
您必须使用MKMapView,禁用用户平移/缩放,添加注释并处理注释上的单击操作以执行所需操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接