来自MapView注释iOS的缩略图图像
如何从mapView中获取缩略图,如下图中红色矩形框所示来自MapView注释iOS的缩略图图像,ios,uiview,annotations,mkmapview,Ios,Uiview,Annotations,Mkmapview,如何从mapView中获取缩略图,如下图中红色矩形框所示 UIView *thumbnailContainer=[[UIView alloc]initWithFrame:CGRectMake(10,10,64,64)]; thumbnailContainer.clipsToBounds=YES; thumbnailContainer.layer.borderColor=[UIColor colorWithWhite:0 alpha:0.5].CGColor; thumbn
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];
很可能这是地图的截图,因为iOS中的任何实时地图都在下角包含谷歌地图徽标( 为了做到这一点,您需要计算出pin所在地图中的rect,然后使用UIGraphicsContent()等函数对视图进行屏幕截图。然后您可以将该图像放入UIImageView中,以便用户可以显示该图像
UIGraphicsBeginImageContext(mapRect);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
或者,您可以使用GoogleMaps静态API,该API允许您使用指定的纬度和经度值在特定位置接收地图的图片
使用缩小的地图视图怎么样?我不认为从地图视图的渲染层创建缩略图是一个好的解决方案,因为它需要首先以所需的缩放加载地图,然后,为什么不直接显示此地图而不是从中生成图像呢?: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];
看起来是这样的:我已经修改了您发布的代码,但是我无法生成输出。请查看上面的代码。如果有任何帮助,将不胜感激。您正在使用的注释对象是什么?它是您创建的某类实际子类吗?在我的示例中,注释只是用来表示注释对象的。我想k静态地图许可证规定只能在网站上使用。当使用iOS5地图工具包创建此缩略图时,必须小心不要隐藏google徽标,否则您的应用程序可能会被拒绝。