gpt4 book ai didi

ios - 放大到 map 上的当前位置

转载 作者:行者123 更新时间:2023-11-28 22:00:28 25 4
gpt4 key购买 nike

我想在 map 上显示用户的当前位置并放大。

我试过如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"Init MapViewController");
[self initMap];
}
return self;
}


-(void) initMap
{
_mapView = [[MKMapView alloc] initWithFrame:self.view.frame];

_mapView.delegate = self;
_mapView.showsUserLocation = YES;

[self.view addSubview:_mapView];

CLLocationCoordinate2D currentLocation;
currentLocation.latitude = _mapView.userLocation.coordinate.latitude;
currentLocation.longitude= _mapView.userLocation.coordinate.longitude;

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(currentLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

[_mapView setRegion:viewRegion animated:YES];
}

然后在模拟器中它显示了海中的一个位置。我在模拟器中将位置设置为伦敦。

最佳答案

只需这样做:

@property  (strong, nonatomic)   CLLocationManager *locationManager;

在viewdidload中:

self.locationManager = [[CLLocationManager alloc] init] ;
self.locationManager.delegate = (id)self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
[self.locationManager setDistanceFilter:10.0f] ;
[self.locationManager startUpdatingLocation];

在您的位置管理器中

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.locationManager stopUpdatingLocation];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = YourMapView.userLocation.location.coordinate.latitude;
zoomLocation.longitude= YourMapView.userLocation.location.coordinate.longitude;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*1609.344, 0.5*1609.344);
// 3
[YourMapView setRegion:viewRegion animated:YES];
}

关于ios - 放大到 map 上的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25283020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com