gpt4 book ai didi

iphone - MKMapView 中的 zoomLevel 和 mapCenter

转载 作者:行者123 更新时间:2023-11-29 11:08:18 25 4
gpt4 key购买 nike

我正在使用 MKMapView 在 iPhone 中创建 map 应用程序.

我确实成功找到了我的 current locationzoom在这一点上是这样的:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.mapView.delegate = self;

self.locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];

[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

[self.mapView setShowsUserLocation:YES];
}

-(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation> mp = [annotationView annotation];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1550, 1550);
[self.mapView setRegion:region animated:YES];
}

我要解决的问题是:

我有一个 locationmap 上(此代码在 viewDidLoad 中):

CLLocationCoordinate2D location = [self getLocationFromAddressString:@"San Francisco, CA, United States"];
// location.latitude = 37.78608;
// location.longitude = -122.407398;

MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Store location" coordinate:location];
[self.mapView addAnnotation:mapAnnotation];

这是mapViewAnnotation:

@implementation MapViewAnnotation

@synthesize title = _title;
@synthesize coordinate = _coordinate;

- (id) initWithTitle:(NSString *) t coordinate:(CLLocationCoordinate2D) c {
self = [super init];
if(self) {
_title = t;
_coordinate = c;
}
return self;
}
@end

我想要一个合适的 zoomLevelmapCenter为此 location和我的 current location .

我可以在 Android 中使用 MapController.zoomToSpan() 成功地做到这一点.如何在 iPhone map 中修复它?

最佳答案

创建位置数组,然后使用所有 AnnotationPins 创建中心 map View

-(void) RoutscenterMap 
{
MKCoordinateRegion region;

CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < arrLocation.count; idx++)// here use your array or points
{
CLLocation* currentLocation = [arrLocation objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}

region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = (maxLat - minLat) * 2;
region.span.longitudeDelta = (maxLon - minLon) * 2;

[mapView setRegion:region animated:YES];
}

你也可以像下面这样在数组中添加位置......

 CLLocation *temploc=[[CLLocation alloc]initWithLatitude:latitude longitude:longtitude];
[arrLocation addObject:temploc];

之后使用这个数组使 map 居中

希望对你有帮助...

:)

关于iphone - MKMapView 中的 zoomLevel 和 mapCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12727520/

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