转到/缩放到当前位置功能(MapKit)

时间:2011-08-11 06:10:30

标签: iphone xcode mapkit currentlocation

我有一个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;
}

这很好用。我添加了一个搜索栏和一个跳转到特定地址位置的函数。这也很好。我现在想要添加一个按钮来跳回当前位置。你可以帮我一把吗?

干杯

5 个答案:

答案 0 :(得分:10)

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

- (IBAction)showCurrentLocation {        
    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}

答案 1 :(得分:2)

您也可以尝试:

mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;

答案 2 :(得分:1)

您可以将此IBAction链接到您的UIButton, 它会在当前位置移动地图并放大它。

if($_SESSION["login"] != 'joaomonteiro' or 'm1n6u3x' or 'jorgesaado17'){
    echo 'Acesso negado, redirecionando...';
    $main = "index.php";
    header('Location: '.$main);
}else{
    echo "success";
}

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

答案 3 :(得分: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];
}

答案 4 :(得分:0)

FOR SWIFT

在按钮操作yourMKMapView.setUserTrackingMode(.follow, animated: true)

中添加此行

确保在yourMKMapView.showsUserLocation = true

中添加viewDidLoad()
相关问题