【问题标题】:how to detect scroll or zoom on mapkit with touch如何通过触摸检测 mapkit 上的滚动或缩放
【发布时间】:2014-05-19 07:45:15
【问题描述】:

我创建了一个 mapkit 应用程序,在此显示我的位置并每次更新此位置并将此位置设置在视图中心。我更新了我的位置并使用布尔变量设置在中心。 我想在我的地图上检测滚动或缩放触摸手指,当我这样做时,我的布尔变量发生了变化,并且没有将我的位置设置在中心,但我不知道如何在 mapkit 上处理触摸事件。

请指导我并告诉我如何在触摸和滚动地图时更改我的变量。 谢谢。 这是我的代码:

@implementation ViewController
{
    CLLocationManager *locationManager;
    double latitudes;
    double longitudes;
    BOOL updateLocation;
}
@synthesize mapView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    updateLocation = YES;
    mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    mapView.mapType = MKMapTypeStandard;
    mapView.zoomEnabled = YES;
    mapView.scrollEnabled = YES;
    mapView.showsUserLocation = YES;
    [mapView.userLocation setTitle:@"I'm Here"];
    [self.view addSubview:mapView];
    mapView.delegate = self;

    [self GetMyLocation];


}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
            NSLog(@"didFailWithError: %@", error);
            UIAlertView *errorAlert = [[UIAlertView alloc]
                                       initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [errorAlert show];
        }
        - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
            CLLocation *currentLocation = newLocation;

            if (currentLocation != nil) {
                longitudes = currentLocation.coordinate.longitude;
                latitudes = currentLocation.coordinate.latitude;
                //NSLog(@"%f,%f",longitudes,latitudes);
                //[self centerLocation];
            }
        }



#pragma mark - MKMapViewDelegate
            - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
                if (updateLocation) {
                    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
                    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
                }
                else{NSLog(@"Don't Update");}
            }

【问题讨论】:

    标签: ios objective-c touch mapkit uitouch


    【解决方案1】:

    只需使用下面的MKMapView 委托方法

    – mapView:regionWillChangeAnimated:
    – mapView:regionDidChangeAnimated:
    

    【讨论】:

      【解决方案2】:

      用途:

      mapView:regionWillChangeAnimated:
      

      告诉代理地图视图显示的区域即将改变。

      每当当前显示的地图区域时调用此方法 变化。在滚动过程中,可能会多次调用该方法 报告地图位置的更新。因此,您的实施 此方法应尽可能轻量级,以免影响 滚动性能。

      基于MKMapViewDelegate Protocol Reference

      【讨论】:

      • 谢谢老兄。有什么不同 – mapView:regionWillChangeAnimated: – mapView:regionDidChangeAnimated:
      【解决方案3】:

      Swift 中,您应该从以下代码中使用

      extension UIViewController: MKMapViewDelegate {
           func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {}
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-02
        • 1970-01-01
        • 2013-04-11
        • 1970-01-01
        相关资源
        最近更新 更多