2011-05-20 73 views
7

我正在iPhone應用程序的MapView中開發自定義引腳。 下面是代碼:在iPhone中顯示用戶位置藍點MKMapView

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString *AnnotationViewID = @"annotationViewID"; 

    MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 

    if (annotationView == nil) 
    { 
     annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease]; 
    } 

    if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")]) { 

     MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease]; 
     //pin.pinColor = MKPinAnnotationColorRed; 
     annotationView = pin; 
    } 
    else 
    { 
     NSString *pin = [NSString stringWithFormat:@"pin%i.png",[[annotation imgNumber] intValue]]; 
     annotationView.image = [UIImage imageNamed:pin];//Canviar la chincheta per numero 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [annotationView setRightCalloutAccessoryView:button]; 
     annotationView.annotation = annotation; 
     annotationView.canShowCallout = YES; 

    /********* Imagen a la izquierda en la burbuja *********/ 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    // Set up the Left callout 
    UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    myDetailButton.frame = CGRectMake(0, 0, 23, 23); 
    myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 

    // Set the image for the button 
    //[myDetailButton setImage:[UIImage imageNamed:@"botocorreu.png"] forState:UIControlStateNormal]; 
    NSString *detailButton = [NSString stringWithFormat:@"%i",[[annotation imgNumber] intValue]]; 
    [myDetailButton setImage:[UIImage imageNamed:detailButton] forState:UIControlStateNormal]; 
    // Set the button as the callout view 
    annotationView.leftCalloutAccessoryView = myDetailButton; 
    annotationView.canShowCallout = YES; 
    } 
    return annotationView; 
    } 

這工作不錯,但讓我與紅色的大頭針用戶的位置。 我檢查IB中的「顯示用戶位置」。 我想使用默認的藍色點,當用戶移動時會自動移動它,不正確? 我該怎麼做?

非常感謝。

回答

22

此添加到viewForAnnotation方法的頂部

if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

特殊用戶的位置的註釋是MKUserLocation類型的,並且在這種情況下返回nil告訴地圖視圖繪製默認視圖它其是藍點。

可以刪除這些行,因爲他們將不再需要:

if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")]) { 
    MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease]; 
    //pin.pinColor = MKPinAnnotationColorRed; 
    annotationView = pin; 
} 
else 
+0

它的作品好,thnks。其中最重要的是顯示用戶位置的藍色點,在他周圍形成一個非常大的圈,是否可以成本高昂? – ValentiGoClimb 2011-05-21 15:22:58

+0

圓圈大小表示位置的精確度(更小意味着更準確)。不知道這是否可以使用CLLocationManager在MKMapView中進行控制。 – Anna 2011-05-21 16:15:24

+0

好吧。而且您知道如何通過代碼手動引入userlocation。我需要把用戶放在一個地方。 throat – ValentiGoClimb 2011-05-22 18:10:12

相關問題