简体   繁体   中英

How to showUserLocation without animation in MapView

I use this code to convert MKMapView to UIImage:

+(UIImage *)imageFromMapView:(MKMapView *)mapView {

    BOOL showsUserLocation = mapView.showsUserLocation; //save flag
    [mapView setShowsUserLocation:NO]; //hide blue bubble for taking picture

    CGRect frame = mapView.frame;
    if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
        ([UIScreen mainScreen].scale == 2.0)) {
        frame.size.width *= 2;
        frame.size.height *= 2;
    }
    //    UIGraphicsBeginImageContext(frame.size);
    UIGraphicsBeginImageContextWithOptions((frame.size), YES, 0.0);
    [mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    DLog(@"got mapImage size:%@",NSStringFromCGSize(mapImage.size));

    [mapView setShowsUserLocation:showsUserLocation];//restore flag
    return mapImage;
}

I call this method everytime i move MapView center position, so yellow bubble appears with whole animation in every step. It is annoying.

Is it possible to show current user location without animation?

我不这么认为,用相同的区域创建一个新的脱机MKMapView等等,但是如果没有用户位置来获取快照,则每次用户移动可见地图时,成本可能都太高了。

short answer: you can't -- not without SOME effort :)

long answer: you can't UNLESS you fake the whole thing and return a blank annotation view for the real location and use the location manager to update another custom annotation.

steps:

1 - if viewForAnnotation asks for a UIView for the MKUserLocation , return a blank one but not nil! Use An empty UIView.
nil would show the dot.

NOW returning ANYTHIN BUT nil from viewForAnnotation for the location will disable tracking: this is a serious bug in map kit

2 - so you need to start the CLLocationManager yourself, add an OWN MKAnnotation to the MKMapView in order to show the userLocation.
That you can treat like ANY MKAnnotation updating its coordinate whenever locationDidUpdate is fired

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM