iphone开发:修改地图蓝色用户位置
将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
补充:移动开发 , IOS ,