我有个问题。我的当前位置在地图视图中显示并居中,但是地图区域不会放大。我尝试采纳了Rob的建议,从didUpdateToLocation方法中去掉了span和region,但我肯定没有正确地实现它。我认为它不能识别我在viewDidLoad中对setRegion的调用,也不能识别我的按钮。请检查我下面的代码并指出错误。我的目标是能够使用IBAction按钮放大和缩小我所在的位置。
.h
- (IBAction)zoomIn:(id)sender;
- (IBAction)zoomOut:(id)sender;viewDidLoad中的.m
double miles = 0.5;
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/69.0;
MKCoordinateRegion region;
region.span = span;
[self.mapView setRegion:region animated:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
_mapView.mapType = MKMapTypeSatellite;我的didUpdateToLocation方法中的.m。
[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];.Zoom In:
- (IBAction)zoomIn:(id)sender
{
MKCoordinateSpan span;
span.latitudeDelta = _mapView.region.span.latitudeDelta * 2;
span.longitudeDelta = _mapView.region.span.latitudeDelta * 2;
MKCoordinateRegion region;
region.span = span;
region.center = _mapView.region.center;
[self.mapView setRegion:region animated:YES];
}.Zoom输出:
- (IBAction)zoomOut:(id)sender
{
MKCoordinateSpan span;
span.latitudeDelta = _mapView.region.span.latitudeDelta / 2;
span.longitudeDelta = _mapView.region.span.latitudeDelta / 2;
MKCoordinateRegion region;
region.span = span;
region.center = _mapView.region.center;
[self.mapView setRegion:region animated:YES];
}发布于 2013-03-03 03:28:52
您可以获取当前的region,根据需要将span乘以或除以2(放大时除以,缩小时乘以),然后设置region
- (IBAction)zoomIn:(id)sender {
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta /= 2.0;
region.span.longitudeDelta /= 2.0;
[self.mapView setRegion:region animated:YES];
}
- (IBAction)zoomOut:(id)sender {
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta = MIN(region.span.latitudeDelta * 2.0, 180.0);
region.span.longitudeDelta = MIN(region.span.longitudeDelta * 2.0, 180.0);
[self.mapView setRegion:region animated:YES];
}如果您希望地图自动缩放到您的位置,您可以使用:
self.mapView.userTrackingMode = MKUserTrackingModeFollow;如果您想手动完成此操作,可以执行以下操作。首先,为您是否已经放大定义一个类属性:
@property (nonatomic) BOOL alreadySetZoomScale;然后更改didUpdateLocations (或didUpdateToLocation)以选中此选项,并设置缩放比例一次:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* newLocation = [locations lastObject]; // if less than zero, then valid lat and long not found
if (newLocation.horizontalAccuracy < 0)
return;
if (!self.alreadySetZoomScale)
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1609, 1609); // 1 mile = 1609.34 meters
self.mapView.region = region;
[self.mapView setRegion:region animated:YES];
self.alreadySetZoomScale = YES;
}
else
{
[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// if prior to iOS 6, use this old `MKMapViewDelegate` method, but call our
// other routine.
if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
[self locationManager:manager didUpdateLocations:@[newLocation]];
}这基本上是说,“如果我还没有放大,根据newLocation和该位置周围一定的米数来设置区域,但如果我已经这样做了,就根据我当前的位置设置中心坐标,但不要改变缩放比例(如果我已经放大或缩小了)。这也做了一些有条件的iOS版本号逻辑(使用macros shown here),以确保最终用户运行的是iOS 6.0之前的版本,它将调用我们的更新方法。
顺便说一下,如果你在地图上显示用户位置(例如self.mapView.showsUserLocation = YES;),你可能想让这个didUpdateLocations在移动地图中心之前也移除与MKUserLocation相关的覆盖,否则它可能会留下旧的覆盖:
[self.mapView removeOverlays:self.mapView.overlays];发布于 2013-03-03 02:13:51
尝尝这个。但我还没有测试过。
- (IBAction)zoomIn:(id)sender {
MKCoordinateSpan span;
span.latitudeDelta = _mapView.region.span.latitudeDelta * 2;
span.longitudeDelta = _mapView.region.span.latitudeDelta * 2;
MKCoordinateRegion region;
region.span = span;
region.center = _mapView.region.center;
[self.mapView setRegion:region animated:YES];
}
- (IBAction)zoomOut:(id)sender {
MKCoordinateSpan span;
span.latitudeDelta = _mapView.region.span.latitudeDelta / 2;
span.longitudeDelta = _mapView.region.span.latitudeDelta / 2;
MKCoordinateRegion region;
region.span = span;
region.center = _mapView.region.center;
[self.mapView setRegion:region animated:YES];
}发布于 2013-03-07 08:51:51
好了,这是我最后用来让我的两个IBAction按钮(zoomIn和zoomOut)与我的mapView一起工作的东西。打开应用程序时,将显示用户位置,地图将放大并围绕其居中。然后你可以使用按钮放大或缩小2倍。*非常感谢Rob给我指路。
.h
- (IBAction)zoomIn:(id)sender;
- (IBAction)zoomOut:(id)sender;.m
@interface LocationViewController ()
@property (nonatomic) BOOL alreadySetZoomScale;
@end
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if (!_alreadySetZoomScale)
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1609, 1609); // 1 mile = 1609.34 meters
self.mapView.region = region;
[self.mapView setRegion:region animated:YES];
_alreadySetZoomScale = YES;
}
else
{
[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
}
- (IBAction)zoomIn:(id)sender {
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta /= 2.0;
region.span.longitudeDelta /= 2.0;
self.mapView.region = region;
}
- (IBAction)zoomOut:(id)sender {;
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta *= 2.0;
region.span.longitudeDelta *= 2.0;
self.mapView.region = region;
}在viewDidLoad中,我还有其他几个对MKMapView的调用,比如self.mapView.showsUserLocation = YES;,但差不多就这些了。
https://stackoverflow.com/questions/15177400
复制相似问题