2011-07-19 22 views
0

我正在開發一個應用程序來顯示特定郵政編碼中的商店。我從當前位置開始。我正在爲當前位置獲得藍色脈動點。當我輸入郵編時,我可以在地圖視圖中顯示註釋。我有一個按鈕「當前位置」,可以再次返回到當前位置。然而,第二次我沒有得到藍點?我如何得到它,這是代碼:mapview藍點不再來第二次

- (void)showCurrentLocation 
{ 
self.mapView.showsUserLocation = YES; 
    GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:self.mapView.userLocation.coordinate]; 

    annotation.title = @"Location"; 
    [self.mapView addAnnotation:annotation]; 
    [annotation release];  

    MKCoordinateRegion zoomIn; 
    zoomIn.center.latitude = self.mapView.userLocation.coordinate.latitude; 
    zoomIn.center.longitude=self.mapView.userLocation.coordinate.longitude; 

    zoomIn.span.latitudeDelta = .1f; 
    zoomIn.span.longitudeDelta =.1f; 

    self.mapView.showsUserLocation = YES; 
    [self.mapView setRegion:zoomIn animated:TRUE]; 
} 

// annotation view 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(GPSAnnotation *)annotation 
{ 
    if ([[annotation title] isEqualToString:@"Location"]) 
    { 
     MKAnnotationView *annotationView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease]; 

     annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"]; 
     annotationView.annotation = annotation; 
     return annotationView; 
    } 

    else if ([annotation isKindOfClass:[MKUserLocation class]]) 
    {   
     return nil; 
    } 
} 
+0

爲什麼將GPSAnnotation座標設置爲與用戶位置相同? viewForAnnotation方法必須給你編譯器警告「控制達到非void函數的結束」。至少在方法的末尾添加'return nil;'來解決這個問題。除此之外,還有其他一些問題。 – Anna

+0

您好Anna Karenina, 感謝您的回覆。我試着添加「return nil」,即使那樣我也不會爲第二個tym獲得tat點。 – cancerian

回答

4

你做一個地方[self.mapView removeAnnotations:self.mapView.annotations]清空地圖嗎?這是一個常見的錯誤,因爲你也通過這樣去除了userlocation。

+0

hi yinkou, 感謝您的回覆。你好,我正在刪除它。但不是用來將註釋添加到用戶當前位置的方法嗎? – cancerian

+0

是的,你可以通過[mapView addAnnotation:mapView.userlocation]重新添加它; 但是最好不要刪除它,因爲每次將它重新添加到地圖中時,它都會生成動畫,並且可能會導致UI變差。 這樣做會更好: 'NSMutableArray * annotationsToRemove = [[NSMutableArry alloc] initWithArray:mapView.annotations]; [annotationsToRemove removeAnnotation:mapView.userLocation]; [mapView removeAnnotations:annotationsToRemove];' 這將從地圖中移除除userLocation以外的所有註釋。 – yinkou