【问题标题】:Thumbnail image from MapView Annotation-iOS来自 MapView Annotation-iOS 的缩略图
【发布时间】:2012-09-28 16:27:10
【问题描述】:

您如何从 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];

【问题讨论】:

  • 使用 iOS5 地图工具包创建此缩略图时,请注意不要隐藏 google 徽标,否则您的应用可能会被拒绝。

标签: ios uiview annotations mkmapview


【解决方案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];

看起来像这样:

【讨论】:

  • 我已经修改了您发布的代码,但无法生成输出。请参阅我上面的代码。任何帮助将不胜感激?
  • 您使用的这个 Annotation 对象是什么?它是您创建的某种实际子类吗?在我的示例中,注释只是用来表示注释对象。
【解决方案2】:

它可能是地图的屏幕截图,因为 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/

【讨论】:

  • 我认为静态地图许可证规定它只能在网站上使用
猜你喜欢
  • 2017-10-21
  • 2015-03-27
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
  • 2020-07-07
  • 1970-01-01
相关资源
最近更新 更多