2012-05-29 17 views
8

estoy usando el marco MapKit para cargar los mapas de Google en mi solicitud y me coloco en un mapa 4 "simulado" lugares como esto:Cómo usar iconos personalizados con mapKit framework?

- (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];  

}

El código es bastante simple. Lo que quiero hacer es, en lugar de usar el pin rojo típico en mi mapa de google, usar mis propias imágenes para mostrar diferentes lugares. ¿Existe una manera simple/directa de hacerlo, porque me confundí mucho de las muestras que ya encontré?

+1

¿Cuál es la confusión exacta? ¿Qué has intentado? – Anna

Respuesta

11

Usted puede cambiar la imagen de anotación mediante el uso de código siguiente,

- (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; 
} 

donde "marker.png" es el archivo de imagen.

+0

Muchas gracias por su respuesta. Ok, esta es una función que me devuelve el poi en el mapa con una imagen. ¿Pero cómo lo llamo para cada poi diferente? No entiendo cómo debería usar esta función. ¿Simplemente lo copié en el archivo viewcontroller? Lo siento, soy muy nuevo en el desarrollo de iOS. – donparalias

+0

Supongo que en lugar de usar: [mapView addAnnotation: poi1]; debería usar: [mapView viewForAnnotation: poi1]; ¿verdad? – donparalias

+0

No, no necesita llamar a este método. Este es uno de los métodos delegados de MKAnnotation. Este método se llama automáticamente. Acabo de actualizar el código anterior para cambiar la imagen de cada anotación (poi). Por favor, consulte el código. Gracias. – Nandha

Cuestiones relacionadas