【问题标题】:Weird behaviour in MKMapView - pins disappearingMKMapView 中的奇怪行为 - 引脚消失
【发布时间】:2012-11-06 19:29:46
【问题描述】:

在我的应用中,我们有一个带有 MKMapView 的屏幕。

此地图显示了一组位置的图钉(位置是在应用程序中定义的模型)。

在模拟器中运行时,效果很好。但是,当在设备上运行此程序时,几乎每次平移地图时,引脚似乎都会消失并重新出现。即使是轻微的触摸和移动也会导致地图显示和隐藏图钉。

地图不是特别忙,即使显示一个图钉它也会显示和隐藏它。

有人知道为什么会发生这种情况吗?我在下面粘贴了我的代码...这是一个 Rubymotion 应用程序,所以代码是在 Ruby 中的。

- 更新-

我在下面添加了与此 Ruby 代码等效的 Objective C。抱歉,如果有一些拼写错误或惯用错误,我已经有一段时间没有写任何 OC了。

- 更新 2 -

查看记录器,我可以看到每次地图图钉消失/重新出现时都会调用 mapView:viewForAnnotation

而且每次mapView:regionDidChangeAnimated 我都可以看到注释的对象 ID 是相同的 - 所以我认为它们不会被删除(它们不应该被删除)

# ====================
# = MKMapKitDelegate =
# ====================

# Don't react if the user has moved less than three meters
USER_MOVE_THRESHOLD = 3

# The user location has changed
def mapView(mapView, didUpdateUserLocation: newLocation)
  NSLog("mapView:didUpdateUserLocation")
  return unless userLocation
  coord = newLocation.coordinate
  newLocationAsCL = CLLocation.alloc.initWithCoordinate(coord, altitude: 1, horizontalAccuracy:1, verticalAccuracy: -1, timestamp: nil)    
  meters = newLocationAsCL.distanceFromLocation(@lastUserCLLocation)

  # If user has moved less than 3m, return
  if meters > 0 and meters < USER_MOVE_THRESHOLD
    log "Distance was less than #{USER_MOVE_THRESHOLD} meters (#{meters}) - returning ***"
    return
  end

  # If the coord is the same as the previous user location
  if userLocation.coordinate.latitude == coord.latitude && userLocation.coordinate.longitude == coord.longitude
    log "User hasn't moved - returning ***"
  else
    log 'User has moved'
  end

  log "Did update user location: #{coord.latitude},#{coord.longitude}"

  if coord.latitude.to_f == 0.0 and coord.longitude.to_f == 0.0
    log 'Invalid coordinate received - returning ***'
  else
    fetchLocationsFromAPI
  end
end

def mapView(mapView, regionDidChangeAnimated: animated)
  NSLog("mapView:regionDidChangeAnimated:#{animated}")
  # do nothing here yet...
end

# create map pins...
def mapView(mapView, viewForAnnotation: annotation)
  log "mapView:viewForAnnotation: #{annotation.inspect}"

  if annotation.is_a?(Location)
    # If there's already an annotation we can use, use it! Otherwise create a new one
    annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotation.class.to_s) || begin
      annotationView = MKPinAnnotationView.alloc.initWithAnnotation(annotation, reuseIdentifier: annotation.class.to_s)
      annotationView.enabled        = true
      annotationView.canShowCallout = true
      annotationView.animatesDrop   = false
      annotationView.pinColor = MKPinAnnotationColorRed

      rightButton = UIButton.buttonWithType(UIButtonTypeDetailDisclosure)
      rightButton.addTarget(self, action: 'showLocationScreen:', forControlEvents: UIControlEventTouchUpInside)
      annotationView.rightCalloutAccessoryView = rightButton      
      annotationView      
    end
    annotationView.annotation = annotation
    annotationView.rightCalloutAccessoryView.tag = @mapLocations.index(annotation)
    return annotationView
  end
end

def mapView(mapViewm, didAddAnnotationViews: views)
  NSLog("mapView:didAddAnnotationViews - #{views}")
  # do nothing here yet...
end

目标 C

#define kUserMoveThreshold 1

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)newLocation
{
  NSLog(@"mapView:didUpdateUserLocation")
  if (!userLocation) {
    return;
  }
  CLLocationCoordinate2D coord = newLocation.coordinate;
  CLLocation newLocationAsCL = [[CLLocation alloc] initWithCoordinate: coord altitude: 1 horizontalAccuracy: 1 verticalAccuracy: -1 timestamp: NULL];
  CLLocationDistance meters = [newLocationAsCL distanceFromLocation: lastUserCLLocation];

  // If user has moved less than 3m, return
  if (meters > 0 && meters < kUserMoveThreshold){
    NSLog(@"Distance was less than %d meters (%d) - returning ***", kUserMoveThreshold, meters);
    return;
  }

  // If the coord is the same as the previous user location
  if (userLocation.coordinate.latitude == coord.latitude && userLocation.coordinate.longitude == coord.longitude){
    NSLog(@"User hasn't moved - returning ***");
    return;
  } else {
    NSLog(@"User has moved");  
  }
  NSLog(@"Did update user location: %f,%f", coord.latitude, coord.longitude);
  if (coord.latitude == 0.0 && coord.longitude == 0.0){
    NSLog(@"Invalid coordinate received - returning ***");
  } else {
    [self fetchLocationsFromAPI];
  }
}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
  NSLog(@"mapView:regionDidChangeAnimated: %s", animated ? @"TRUE" : @"FALSE");
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
  NSLog(@"mapView:viewForAnnotation %s", annotation.description);

  if ([annotation isKindOfClass: [Location class]]){
      // If there's already an annotation we can use, use it! Otherwise create a new one
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier: [annotation className]];
    if (!annotationView){
      annotationView = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: [annotation className]];
      [annotationView setEnabled: YES];
      [annotationView setCanShowCallout: YES];
      [annotationView setAnimatesDrop: NO];
      [annotationView setPinColor: MKPinAnnotationColorRed];

      UIButton *rightButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
      [rightButton addTarget: self action: @selector(showLocationScreen:) forControlEvents: UIControlEventTouchUpInside];
      [annotationView setRightCalloutAccessoryView: rightButton];
    }

    [annotationView annotation: annotation];
    [[annotationView rightCalloutAccessoryView] setTag: [mapLocations indexOfObject: annotation]];
    return annotationView
  }
}

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
  NSLog(@"mapView:didAddAnnotationViews %@", views)
}

【问题讨论】:

  • didUpdateUserLocation 在用户平移地图时可能会被调用,也可能不会被调用。显示设置地图并添加注释的代码可能会更有用。 viewForAnnotations 也是另一个相关的。以及任何响应缩放或使用“addAnnotation”或removeAnnotation的代码
  • 我在使用 ruby​​motion 时也遇到了同样的问题。即使在模拟器上,大头针也会随机消失和重新出现。我正在尝试延迟加载注释。有什么突破吗?
  • 什么都没有 - 你使用的是 Rubymotion 还是 Objective C/Xcode?​​span>

标签: objective-c ios mkmapview rubymotion


【解决方案1】:

这是 Rubymotion 的一个错误,似乎已在 1.30 版中解决

= RubyMotion 1.30 =

...

  • 修复了 MapKit 注释针会消失的错误,因为某些 Ruby 对象(在本例中为 MKAnnotations)将使用 Bignum 值作为 `hash' 方法的返回值,使用时无法正常工作 在 MapKit 中。

...

【讨论】:

  • 谢谢 - 忘了这个问题
【解决方案2】:

当你创建pin注解时,你是把它们添加到mapView的注解数组中吗?

CLLocationCoordinate2D locationForSelectedFloorOffice;
locationForSelectedFloorOffice.longitude = [[myGeoCode objectAtIndex:0] floatValue] * 1.0;
locationForSelectedFloorOffice.latitude = [[myGeoCode objectAtIndex:1] floatValue] * 1.0;
MyLocation *annotation = [[MyLocation alloc] initWithName:dStore.selectedFloorRoomName address:@" " coordinate:locationForSelectedFloorOffice];
[self.mapView addAnnotation:annotation];

【讨论】:

  • 是的 - 我应该提到这一点。 Location 定义了标题和副标题,使其符合 MKAnnotation 并且在控制器中的其他位置 [self.mapView addAnnotation: locations] 被调用。这发生在 API 调用之后,并且当引脚消失/重新出现时不会再次调用该方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 2017-12-07
相关资源
最近更新 更多