【问题标题】:"Show current location" Button (Like Maps.app)“显示当前位置”按钮(如 Maps.app)
【发布时间】:2011-09-09 16:36:47
【问题描述】:

我声明我是初学者,所以请不要错过任何一步! 我使用该代码显示并“跳转”到用户位置:

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
    if(annotationView.annotation == mv.userLocation) {
        MKCoordinateRegion region;
        MKCoordinateSpan span;

        span.latitudeDelta=0.002;
        span.longitudeDelta=0.002; 

        CLLocationCoordinate2D location=mv.userLocation.coordinate;

        region.span=span;
        region.center=location;

        [mv setRegion:region animated:TRUE];
        [mv regionThatFits:region];
    }
}

但是我想在我按下按钮时请求它。我该怎么做? (记住:我是初学者!)。提前致谢!

【问题讨论】:

    标签: iphone mkmapview core-location


    【解决方案1】:

    您需要做的就是不要在上面调用您的 setRegion,而是在按下按钮时调用它。或者,您可以使用 [locationManager startUpdatingLocation][locationManager stopUpdatingLocation] 开启和关闭更新地图,但保持开启状态会消耗电池电量,因此请谨慎行事。

    【讨论】:

    • 我尝试使用此代码但仍然无法正常工作(当我按下按钮时它会冻结)。 -(IBAction)goToLocation { MKMapView *mapView; MKUserLocation *myLocation = [mapView userLocation]; CLLocationCoordinate2D coord = [[myLocation location] coordinate]; MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 350, 350); [mapView setRegion:region animated:YES]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1]; [UIView commitAnimations]; }
    • 去掉对 beginAnimations、setAnimationDuration 和 commitAnimations 的调用。你已经告诉你的 setRegion 调用动画,它会比你在这里做的更好。如果它仍然有问题,请使用调试器,看看它冻结在哪一行。
    • 您可能还想在其中添加这些行: region.span=span; region.center=location;
    猜你喜欢
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2020-03-09
    • 1970-01-01
    • 2015-05-13
    相关资源
    最近更新 更多