繁体   English   中英

如何在iOS中将当前位置用户Avtar居中

[英]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图片位于地图中心。 当我在我的profileImageView框架上放置(-30,-30)坐标时。

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

profileImageView.layer.masksToBounds = YES;

profileImageView.layer.cornerRadius = 27;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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