2011-03-15 38 views
0

我嘗試在我的應用程序創建的地圖部分節目,但由於某些原因註釋不會將正確的MKMapView滑稽Buissness

MKMapView *mapView = (MKMapView*)self.view; 

CLLocationCoordinate2D coordinate; 

coordinate.latitude = 55.065767; 

coordinate.longitude = -2.724609; 



mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 500000, 500000); 
mapView.delegate = self; 


for (int i=0; i<1; i++) { 

    coordinate.latitude = latitude; 
    coordinate.longitude = longitude; 
    CLLocationCoordinate2D newCoord = {coordinate.latitude, coordinate.longitude}; 
    PlaceAnnotation* annotation = [[PlaceAnnotation alloc] initWithCoordinate:newCoord]; 
    //annotation.mTitle = companyTitle; 
    //annotation.mSubtitle = companyDescription; 
    [mapView addAnnotation:annotation]; 

    [annotation release]; 

} 


} 

,並在我的地方annotaion

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate{ 

self = [super init]; 

if (self != nil) { 

    _coordinate = coordinate; 



} 

return self; 

} 

lattitude和longditude都有正確的值,我以前使用過這個代碼,但使用指向一個對象的數組,它工作正常,但是當我嘗試直接給它的值時,它會失敗

誰能幫我出

*編輯,對不起,我已經有我Viewforannotation剛剛在副本中錯過了

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
MKPinAnnotationView *pinAnnotation = nil; 
if(annotation != mV.userLocation) 
{ 
    static NSString *defaultPinID = @"myPin"; 
    pinAnnotation = (MKPinAnnotationView *)[mV dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (pinAnnotation == nil) 
     pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 

    pinAnnotation.canShowCallout = YES; 




    //instatiate a detail-disclosure button and set it to appear on right side of annotation 

} 

return pinAnnotation; 
[pinAnnotation release]; 
} 

回答

1

您的PlaceAnnotation需要實現MKAnnotation協議。具體而言,該方法

- (CLLocationCoordinate)coordinate; 

,或者你可以定義屬性,並將其設置在構造函數

@property (assign) CLLocationCoordinate coordinate 

這是怎樣的MKMapView知道在何處放置註解。

p.s. 此外,我不知道如何猶太是這樣的:

CLLocationCoordinate2D newCoord = {coordinate.latitude, coordinate.longitude}; 

這也許很好,但蘋果公司推薦:

CLLocationCoordinate2D newCoord = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude); 
1

您需要實現MKMapViewDelegate協議方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 

供應一個MKAnnotationView實例(或子類)用來顯示註釋。詳情請參閱MKMapViewDelegate protocol reference