如何在 ios 中将当前位置用户头像居中

How to center current location user avtar in ios

我需要在MKView上集中显示当前位置的用户图片。但目前它不在中心,如下面的屏幕截图所示。

-

 (void)locationManager:(CLLocationManager *)manager didFailWithError:   (NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);

    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil)
    {
        NSLog(@"%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);

        NSLog(@"%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);

       // [mkMapView setCenterCoordinate:mkMapView.userLocation.location.coordinate animated:YES];
    }
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 150, 150);

    [mkMapView setRegion:[mkMapView regionThatFits:region] animated:YES];
}
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aLocation {

MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = aLocation .coordinate.latitude;
location.longitude = aLocation .coordinate.longitude;
region.span = span;
region.center = location;
[aMapView setRegion:region animated:YES];

}

MKCoordinateSpan span;
MKCoordinateRegion region;

span.longitudeDelta =0.001;
span.latitudeDelta =0.001;

region.span = span;

region.center.latitude = self.mapVIew.region.center.latitude;
region.center.longitude = self.mapVIew.region.center.longitude;
[self.mapVIew setRegion:region];

会帮忙

使用此代码,

[yourMapViewName setCenterCoordinate:yourMapViewName.userLocation.location.coordinate animated:YES];

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 100, 100);
    [yourMapViewName setRegion:[yourMapViewName regionThatFits:region] animated:YES];
}

希望对您有所帮助

使用下面的委托方法,它对我有用,

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    //Get the east and west points on the map so you can calculate the distance (zoom level) of the current map view.
    MKMapRect mRect = YourMapViewName.visibleMapRect;
    MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
    MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));

    //Set your current distance instance variable.

    //Set your current center point on the map instance variable.
    coordinate = YourMapViewName.centerCoordinate;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    self.locationManager1.distanceFilter = kCLDistanceFilterNone;
    self.locationManager1.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager1 startUpdatingLocation];

    //View Area
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };

    region.center.latitude =coordinate.latitude;

    region.center.longitude=coordinate.longitude;
    region.span.longitudeDelta = 0.005f;
    region.span.longitudeDelta = 0.005f;
    [YourMapViewName setRegion:region animated:YES];

}

希望对您有所帮助

试试这个,希望会有帮助。

 MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
 region.center.latitude = [yourLatitude doubleValue];//locationManager.location.coordinate.latitude;
 region.center.longitude = [yourLongitude doubleValue];// locationManager.location.coordinate.longitude;
 region.span.latitudeDelta = 0.005f;
 region.span.longitudeDelta = 0.005f;
 [yourMapView setRegion:region animated:YES];

我找到了解决方案。现在我的 fb 图片出现在地图的中心。当我将 (-30,-30) 坐标放在我的个人资料 ImageView 框架上时。

profileImageView.frame = CGRectMake(-30, -30, 54, 54);

profileImageView.layer.masksToBounds = YES;

profileImageView.layer.cornerRadius = 27;