使用Pin添加UIMapView缩略图

时间:2012-06-30 17:46:02

标签: iphone objective-c mkmapview

我一直在努力实现像这张照片中的UIMapView图标。有人知道这个应用程序是如何创建缩略图的吗?

修改

尝试使用以下代码实现此目的:

CLLocationCoordinate2D restaurantLocation = CLLocationCoordinate2DMake(Latitude, Longitude);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(restaurantLocation, 1000, 1000);

_mapView.region = region;
_mapView.hidden = NO;

_annotation = [[MKPointAnnotation alloc] init];
_annotation.coordinate = restaurantLocation;
//annotation.title = _restaurantInformation 

[_mapView addAnnotation:_annotation];


UIGraphicsBeginImageContext(_mapView.frame.size);
[_mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGSize newSize;
newSize.width = 50; // any width u want
newSize.height= 80; // any height u want
UIGraphicsBeginImageContext(newSize);
[mapImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
_mapThumbnail.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

enter image description here

2 个答案:

答案 0 :(得分:1)

Mapview中的位置注释应位于中心。现在

UIGraphicsBeginImageContext(mapView.frame.size);
[mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

根据

创建mapImage的缩略图
CGSize newSize;
newSize.width = 50; // any width you want
newSize.height= 80; // any height you want
UIGraphicsBeginImageContext(newSize);
[mapImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
UIImage* thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

另一种方式是

您可以尝试Google Static Maps 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 *thumbnail= [UIImage imageWithData:data];
[yourImageView setImage:thumbnail];

通过这个,您可以根据您的坐标获得静态图像。

答案 1 :(得分:1)

此代码显示特定位置的缩略图及其为我工作

NSString *staticMapUrl = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?markers=color:red|%f,%f&%@&sensor=true",lat, log,@"zoom=10&size=114x116"];
NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:mapUrl]];
[imgViewClue setImage:image];