0

您如何从 mapView 获取缩略图,如下图红色矩形框所示。在此处输入图像描述

UIView *thumbnailContainer=[[UIView alloc]initWithFrame:CGRectMake(10,10,64,64)];
    thumbnailContainer.clipsToBounds=YES;
    thumbnailContainer.layer.borderColor=[UIColor colorWithWhite:0 alpha:0.5].CGColor;
    thumbnailContainer.layer.cornerRadius=3;
    thumbnailContainer.layer.borderWidth=1;

    Annotation *annotation1=[[Annotation alloc]init];
    annotation1.coordinate = CLLocationCoordinate2DMake(29.7189214,-95.33916);
    annotation1.title = @"University of Houston";
    annotation1.subtitle = @"4800 Calhoun Rd,Houston";
    MKMapView *mapView2=[[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
    mapView2.userInteractionEnabled=NO;
    mapView2.centerCoordinate=[annotation1 coordinate];
    [mapView2 addAnnotation:annotation1];
    //annPoint=[self.mapView convertCoordinate:annotation1.coordinate toPointToView:self.mapView];

    MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(mapView2.centerCoordinate, 150, 150);
    mapView2.region=region;
    mapView2.transform=CGAffineTransformMakeScale(0.75, 0.75);
    mapView2.layer.position=CGPointMake(thumbnailContainer.bounds.size.width/2,thumbnailContainer.bounds.size.height/2+10);
    [thumbnailContainer addSubview:mapView2];
4

2 回答 2

1

很可能它是地图的截图,因为 iOS 中的任何实时地图都在底角包含 Google 地图徽标(

为此,您需要在地图中计算出图钉所在的矩形,然后使用 UIGraphicsContent() 等函数对视图进行截图。然后,您可以将该图像放入 UIImageView 中,以便用户显示它。

UIGraphicsBeginImageContext(mapRect);    
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

或者,您可以使用 Google Maps Static API,它允许您使用指定的纬度和经度值接收特定位置的地图图片。

https://developers.google.com/maps/documentation/staticmaps/

于 2012-09-28T17:10:23.933 回答
1

使用按比例缩小的地图视图怎么样?我认为从地图视图的渲染层创建缩略图不是一个好的解决方案,因为它需要首先以所需的缩放加载地图,然后,为什么不直接显示此地图而不是制作一个图像出来吗?:D 这是我会使用的代码。

    Annotation *yourAnnotation = [[Annotation alloc] init];

UIView *thumbnailContainer = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 64, 64)];
thumbnailContainer.clipsToBounds = YES;
thumbnailContainer.layer.borderColor = [UIColor colorWithWhite:0 alpha:0.5].CGColor;
thumbnailContainer.layer.cornerRadius = 3;
thumbnailContainer.layer.borderWidth = 1;

MKMapView *thumbnailMap = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
thumbnailMap.userInteractionEnabled = NO;
thumbnailMap.centerCoordinate = [yourAnnotation coordinate];
[thumbnailMap addAnnotation:yourAnnotation];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(thumbnailMap.centerCoordinate, 150, 150);
thumbnailMap.region = region;
thumbnailMap.transform = CGAffineTransformMakeScale(0.75, 0.75);
//we add 10 pixels on the y coordinate, to center the pin in the view. Moreover it will hide the "legal" label of the map view.
thumbnailMap.layer.position = CGPointMake(thumbnailContainer.bounds.size.width / 2, thumbnailContainer.bounds.size.height / 2 + 10);
[thumbnailContainer addSubview:thumbnailMap];

它看起来像这样:在此处输入图像描述

于 2012-09-28T17:16:09.383 回答