2012-06-30 37 views
1

我一直在試圖實現一個像這張圖中的UIMapView圖標。有人知道這個應用程序如何創建縮略圖嗎?添加一個帶引腳的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

回答

1

註釋在圖形頁面與位置應在中心。現在

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

通過這個,你可以獲得基於座標的靜態圖像。

+0

我說我使用的問題,實現的代碼特定位置的縮略圖和它的工作,但它仍然似乎並不奏效。我做對了嗎?謝謝 – Apollo

+0

我已經成功地啓動並運行了MKMapView,但是捕獲當前繪圖並將其設置爲UIImage的方法無效。 – Apollo

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