2012-08-29 56 views
2

我顯示在地圖上的幾個引腳就像這樣:MKAnnotation沒有響應自來水

for (int i = 0; i < points.count; i++) { 
    if([[[points objectAtIndex:i] objectForKey:@"lat"] class] != [NSNull class]) { 
     southWest.latitude = MAX(southWest.latitude , [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]); 
     southWest.longitude = MIN(southWest.longitude, [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]); 
     northEast.latitude = MIN(northEast.latitude, [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]); 
     northEast.longitude = MAX(northEast.longitude, [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]); 

     pin.latitude = [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]; 
     pin.longitude = [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]; 
     CarPin *cPin = [[CarPin alloc] initWithName:[[self.brain.cars objectAtIndex:i] objectForKey:@"name"] state:[self getStateStringFor:[[[points objectAtIndex:i] objectForKey:@"state"] intValue]] coordinate:pin]; 
     cPin.state = [[[points objectAtIndex:i] objectForKey:@"state"] intValue]; 
     cPin.carID = [[points objectAtIndex:i] objectForKey:@"objekt_id"]; 
     [self.mapView addAnnotation:cPin]; 
    } 
} 

CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude]; 
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude]; 
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast]; 

MKCoordinateRegion region; 
region.center.latitude = (southWest.latitude + northEast.latitude)/2.0; 
region.center.longitude = (southWest.longitude + northEast.longitude)/2.0; 
region.span.latitudeDelta = meters/111319.5; 
region.span.longitudeDelta = 0.0; 

[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; 

那麼,我選擇「看」每個引腳這樣的:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
static NSString *identifier = @"CarPin"; 
if ([annotation isKindOfClass:[CarPin class]]) { 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if (annotationView == nil) { 
     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
    } else { 
     annotationView.annotation = annotation; 
    } 
    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    if (((CarPin *)annotation).state == 1) { 
     annotationView.pinColor = MKPinAnnotationColorGreen; 
    } else { 
     annotationView.pinColor = MKPinAnnotationColorRed; 
    } 
    return annotationView; 
} 
return nil; 
} 

我還設置了委託

[self.mapView setDelegate:self]; 

但是,當我點擊每個針腳時,屏幕上沒有顯示細節的氣泡!任何人都有同樣的經歷?

+0

您的CarPin是否實施「title」屬性?我相信你必須爲title屬性設置一個值,否則你將看不到標註。 – Zhang

+0

是的,Car Pin是從iPhone項目中複製而來的,它已經在那裏工作。 – Michal

+1

你必須確保'title'不是零或空白。顯示CarPin.m。另外,CarPin * cPin = [[CarPin alloc] ...'line在這一行中有太多東西讓你或任何人輕鬆理解正在發生的事情,最重要的是你可以輕鬆地調試它 - 將它分成多行。很可能CarPin.m中的title方法返回nil或空白,因爲其中一個init參數爲空。此外,使用MKCoordinateRegionMakeWithDistance,而不是試圖手動設置緯度緯度從米到度。調用regionThatFits也是不必要的。 – Anna

回答

2

僅僅實施title屬性是不夠的。
您必須確保title不返回nil或空白。
如果是,即使canShowCallout設置爲YES,註釋視圖也不會顯示標註。


完全無關的問題,但有關該行:

region.span.latitudeDelta = meters/111319.5; 

這是不推薦的和不必要的,至少有三個原因:

  • 計算兩個角之間的米的距離(爲此你有經度和緯度然後將這些米轉換回)是不必要的,因爲latitudeDeltalongitudeDelta只是頂部/底部或左/右之間的度數差異。所以你所要做的就是:

    region.span.latitudeDelta = fabs(southWest.latitude - northEast.latitude); 
    

    這是我推薦你的情況的改變。

  • 您將數字轉換爲度數的值(111319.5)只能在赤道上精確計算。

  • 如果您想根據米來指定的,而不是計算人工度範圍的區域,這是更好的和更容易使用內置MKCoordinateRegionMakeWithDistance功能:

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake (lat, long); 
    CLLocationDistance latMeters = 5000; //5 km 
    CLLocationDistance lonMeters = 5000; //5 km 
    MKCoordinateRegion region 
        = MKCoordinateRegionMakeWithDistance (center, latMeters, lonMeters); 
    


另外,在您的情況下調用regionThatFits是不必要的,因爲setRegion已經可以自己完成此任務,並且您傳遞的任何區域都是如此。 regionThatFits方法用於您想知道(實際上並未更改區域)地圖視圖將區域調整爲給定某個區域的情況。

+0

這是一個顯着的答案。我非常感謝提示,會做:) – Michal

相關問題