將mkmapview中的用戶當前所在位置的view修改成自己需要的ui。
效果如下:
代碼如下 :
源代碼這裡。全部復制即可。如果缺少某些方法(處理圖片的函數,請留言)
– (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
static NSString* annotation_id = @"annotation_id";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[map_view dequeueReusableAnnotationViewWithIdentifier:annotation_id];
if (!pinView)
{
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotation_id] autorelease];
annotationView.canShowCallout = YES;
//修改地圖的藍色圖片@"flag.png"
UIImage *annotation_image = [UIImage imageNamed:@"flag.png"];
CGRect resizeRect;
resizeRect.size = annotation_image.size;
CGSize maxSize = CGRectInset(map_view.bounds,0.0,0.0).size;
//這一段用於處理圖片,可以省略。這裡的目的是怕此圖片過大。
if (resizeRect.size.width > maxSize.width)
{
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
}
if (resizeRect.size.height > maxSize.height)
{
resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
}
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
//這裡如果你的圖片大小合適,不用處理。直接用你圖片就行瞭。
UIImage *resize_image = [annotation_image imageByScalingAndCroppingForSize:CGSizeMake(45, 45)];
CGRect rect = CGRectMake(0.0, 0.0, 45, 45);
[resize_image drawInRect:rect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
annotationView.image = resizedImage;
annotationView.opaque = NO;
return annotationView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
摘自 雲懷空-abel