9

我有一个 mapView 使用以下命令缩放到当前位置viewDidLoad

#define METERS_PER_MILE 1609.344

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

    mapView.showsUserLocation=TRUE;

    // zoom to  a specific area
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = -28.994167;
    zoomLocation.longitude = 134.866944;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1900*METERS_PER_MILE, 1900*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];    

    // make sure the Google water mark is always visible
    mapView.autoresizingMask =
    (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    [mapView setRegion:adjustedRegion animated:YES];        

    mapView.delegate=self;

    searchBar.delegate = self;
}

这工作正常。我添加了一个搜索栏和一个跳转到特定地址位置的功能。这也很好用。我现在想添加一个按钮来跳回当前位置。请帮帮我好吗?

干杯

4

5 回答 5

11

您需要点击该按钮将地图的中心设置为当前位置。说,像这样:

- (IBAction)showCurrentLocation {        
    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}
于 2011-08-11T06:35:49.693 回答
2

你也可以试试:

mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
于 2013-02-03T10:55:49.097 回答
1

您可以将此 IBAction 链接到您的 UIButton,它将在当前位置移动地图并对其进行缩放。

@IBOutlet weak var mapView: MKMapView!

@IBAction func zoomToUserCurrentLocation(sender: AnyObject) {
    if self.mapView != nil {
        self.mapView.setRegion(MKCoordinateRegionMake(
            self.mapView.userLocation.coordinate, 
            MKCoordinateSpanMake(0.1, 0.1)
        ), animated: true)
    }
}

MKCoordinateSpan 定义了地图区域所跨越的区域,这些值越小,地图越近缩放。

于 2015-05-29T02:17:28.920 回答
0
- (void)showCurrentLocation{

    MKMapPoint annotationPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
    MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.0, 0.0);
    [self.mapView setVisibleMapRect:zoomRect animated:YES];
}
于 2014-07-27T12:54:13.483 回答
0

斯威夫特

在按钮操作中添加此行yourMKMapView.setUserTrackingMode(.follow, animated: true)

确保yourMKMapView.showsUserLocation = true添加viewDidLoad()

于 2017-07-21T02:55:03.163 回答