0

我正在创建一个按钮,该按钮将在用户当前位置放置一个图钉。但是当我按下它时,它会被放置在全球地图的中间,在非洲周围,我相信这是因为在我从中得到这个的原始教程中,他们使用“ didUpdateUserLocation ”创建了一个 void 方法,但我可以想一种将其添加到 IBAction 的方法,这就是正在使用的方法。有没有人遇到过这个问题?

这是代码的放大,但请注意那里的didUpdateUserLocation vvvv

-(void)mapView: (MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
     MKCoordinate region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,   
     800,800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
}

`

现在这里是 IBAction,当点击它时,会在非洲或全球地图的中心放置大头针。我认为这是因为它缺少didUpdateUserLocation

-(IBAction)addAnnotation; 
{
   MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,   
     800,800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];

   MKPointAnnotation *point = [[MKPointAnnotation Alloc] init];
   point.coordinate = myLocation.coordinate;
   point.title = @"Test";
   point.subtitle = @"Text";
   [self.mapView addAnnotation:point];
}

因此,如果有人知道如何,我认为解决方案是 IBAction 的 didUpdateUserLocation,请告诉我,不胜感激!

4

1 回答 1

0

尝试这个...

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation
{
    MKCoordinateRegion region;

    CLLocationCoordinate2D location;
    location.latitude = aUserLocation.coordinate.latitude;
    location.longitude = aUserLocation.coordinate.longitude;

    region.span = aMapView.region.span;
    region.center = location;
    [self.objMapView setRegion:region animated:FALSE];

    showCurrentLoc = FALSE;
}

-(IBAction)addAnnotation;
{
    MKCoordinateRegion region;
    region.span = self.mapView.region.span;
    region.center = location;
    [self.mapView setRegion:region animated:FALSE];

    MKPointAnnotation *point = [[MKPointAnnotation Alloc] init];
    point.coordinate = self.mapView.userLocation.coordinate;
    point.title = @"Test";
    point.subtitle = @"Text";
    [self.mapView addAnnotation:point];
}
于 2014-01-31T11:02:34.933 回答