Why my user location is always in center?

安稳与你 提交于 2019-12-11 07:23:02

问题


i have a map in my app,i set the center user location when map is open. I have used this code:

 map.delegate=Self; 
 ......
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

MKCoordinateRegion mapRegion;
mapRegion.center = self.mapView.userLocation.coordinate;
mapRegion.span = MKCoordinateSpanMake(0.5, 0.5); //Zoom distance
[self.mapView setRegion:mapRegion animated: YES];
}

But when i move map it came back to user location. How can i move free into my map without care user location?


回答1:


This is because you are resetting the location every time the user's location changes. You should do this only once, e.g.

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    if (self.centerToUserLocation)
    {
        MKCoordinateRegion mapRegion;
        mapRegion.center = self.mapView.userLocation.coordinate;
        mapRegion.span = MKCoordinateSpanMake(0.5, 0.5); //Zoom distance
        [self.mapView setRegion:mapRegion animated: YES];
        self.centerToUserLocation = NO;
    }
}

Where centerToUserLocation is something like @property (nonatomic) BOOL centerToUserLocation



来源:https://stackoverflow.com/questions/28497973/why-my-user-location-is-always-in-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!