2012-05-29 44 views
8

我使用MapKit框架加載在我的應用谷歌地圖和我的地方在地圖上4「模擬」的地方是這樣的:如何在mapKit框架中使用自定義圖標?

- (void)viewDidLoad { 

[super viewDidLoad]; 

mapView.delegate = self; 

mapView.showsUserLocation = YES; 

MKUserLocation *userLocation = mapView.userLocation; 

MKCoordinateRegion region = 
MKCoordinateRegionMakeWithDistance (userLocation.location.coordinate,500,500); 
[mapView setRegion:region animated:NO]; 

//Simulated annotations on the map 
CLLocationCoordinate2D poi1Coord , poi2Coord , poi3Coord , poi4Coord; 
//poi1 coordinates 
poi1Coord.latitude = 37.78754; 
poi1Coord.longitude = -122.40718; 
//poi2 coordinates 
poi2Coord.latitude = 37.78615; 
poi2Coord.longitude = -122.41040; 
//poi3 coordinates 
poi3Coord.latitude = 37.78472; 
poi3Coord.longitude = -122.40516; 
//poi4 coordinates 
poi4Coord.latitude = 37.78866; 
poi4Coord.longitude = -122.40623; 

MKPointAnnotation *poi1 = [[MKPointAnnotation alloc] init]; 
MKPointAnnotation *poi2 = [[MKPointAnnotation alloc] init]; 
MKPointAnnotation *poi3 = [[MKPointAnnotation alloc] init]; 
MKPointAnnotation *poi4 = [[MKPointAnnotation alloc] init]; 


poi1.coordinate = poi1Coord; 
poi2.coordinate = poi2Coord; 
poi3.coordinate = poi3Coord; 
poi4.coordinate = poi4Coord; 

poi1.title = @"McDonald's"; 
poi1.subtitle = @"Best burgers in town"; 
poi2.title = @"Apple store"; 
poi2.subtitle = @"Iphone on sales.."; 
poi3.title = @"Microsoft"; 
poi3.subtitle = @"Microsoft's headquarters"; 
poi4.title = @"Post office"; 
poi4.subtitle = @"You got mail!"; 

[mapView addAnnotation:poi1]; 
[mapView addAnnotation:poi2]; 
[mapView addAnnotation:poi3]; 
[mapView addAnnotation:poi4];  

}

的代碼是相當簡單的。我想要做的不是在我的谷歌地圖上使用典型的紅色別針,而是使用我自己的圖像來顯示不同的地方。有沒有一種簡單/直接的方法來做到這一點,因爲我從我已經找到的樣本中感到困惑。

+1

什麼是確切的混淆?你有什麼嘗試? – Anna

回答

11

您可以通過下面的代碼改變標註圖像,

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    if ([[annotation title] isEqualToString:@"Current Location"]) {  
     return nil; 
    } 

    MKAnnotationView *annView = [[MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; 
    if ([[annotation title] isEqualToString:@"McDonald's"]) 
     annView.image = [ UIImage imageNamed:@"mcdonalds.png" ]; 
    else if ([[annotation title] isEqualToString:@"Apple store"]) 
     annView.image = [ UIImage imageNamed:@"applestore.png" ]; 
    else 
     annView.image = [ UIImage imageNamed:@"marker.png" ]; 
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [infoButton addTarget:self action:@selector(showDetailsView) 
      forControlEvents:UIControlEventTouchUpInside]; 
    annView.rightCalloutAccessoryView = infoButton; 
    annView.canShowCallout = YES; 
    return annView; 
} 

其中「marker.png」是你的圖像文件。

+0

非常感謝你的回覆。好的,這是一個函數,可以讓我在地圖上用圖像返回poi。但是,我怎麼稱呼它爲每個不同的poi?我不明白我應該如何使用這個功能。我只是將其複製到viewcontroller文件中?很抱歉,我對ios開發非常陌生。 – donparalias

+0

我猜不是使用: [mapView addAnnotation:poi1];我應該使用: [mapView viewForAnnotation:poi1]; 對不對? – donparalias

+0

不,您不需要調用此方法。這是MKAnnotation的委託方法之一。這個方法被自動調用。我只是更新了上面的代碼來更改每個註釋(poi)的圖像。請參閱代碼。謝謝。 – Nandha

相關問題