Случайный MKPoint Аннотация на карте
В моем проекте я заполняю свою карту этой информацией
for (int i = 0; i<arrayResults.count; i++){
object = [arrayResults objectAtIndex:i];
if ([[object iden]intValue] == 1) {
type = @"type_one";
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
[map addAnnotation:annotationPoint];
}
else if ([[object iden]intValue] == 2) {
type = @"type_two";
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
[map addAnnotation:annotationPoint];
}
else if ([[object iden]intValue] == 3) {
type = @"type_three";
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
[map addAnnotation:annotationPoint];
}
else if ([[object iden]intValue] == 4) {
type = @"type_four";
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
[map addAnnotation:annotationPoint];
}
это четыре типа объектов, которые я добавляю на карту
когда я вхожу в этот метод
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *view = nil;
if (annotation != mapView.userLocation) {
if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 1){
view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"type_one"];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"type_one"];
view.canShowCallout = YES;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[aButton addTarget:self action:@selector(open:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:countPoint+100];
view.rightCalloutAccessoryView = aButton;
view.image = [UIImage imageNamed:@"type_one.png"];
}
}
else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 2){
view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"type_two"];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"type_two"];
view.canShowCallout = YES;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[aButton addTarget:self action:@selector(open:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:countPoint+100];
view.rightCalloutAccessoryView = aButton;
view.image = [UIImage imageNamed:@"type_two.png"];
}
}
else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 3){
view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"type_three"];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"type_three"];
view.canShowCallout = YES;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[aButton addTarget:self action:@selector(open:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:countPoint+100];
view.rightCalloutAccessoryView = aButton;
view.image = [UIImage imageNamed:@"type_three.png"];
}
}
else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 4){
view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"type_four"];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"type_four"];
view.canShowCallout = YES;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[aButton addTarget:self action:@selector(openScheda:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:countPoint+100];
view.rightCalloutAccessoryView = aButton;
view.image = [UIImage imageNamed:@"type_four.png"];
}
}
countPoint++;
}
Проблема в том, что тип маркера не соответствует элементу массива... но я не понимаю. То есть, если у маркера есть изображение "type_one", во всплывающем окне отображается "title" type_four... оно не соответствует
1 ответ
Ваша аннотация должна "знать", какой это тип, чтобы вы могли создать правильный вид в viewForAnnotation. Сделайте это, создав подкласс MKPointAnnotation, добавив к нему значение NSInteger для хранения индекса массива.
Посмотрите MKPointAnnotation добавить дополнительное свойство, но вы добавите свойство NSInteger с именем index.
При создании ваших аннотаций установите это свойство:
for (int i = 0; i<arrayResults.count; i++)
{
object = [arrayResults objectAtIndex:i];
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);
// MKMyPointAnnotation is your MKPointAnnotation subclass
MKMyPointAnnotation *annotationPoint = [[MKMyPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
annotationPoint.index = i; // store the index
[map addAnnotation:annotationPoint];
}
Затем в вашем viewForAnnotation приведите аннотацию к вашему подклассу, а затем разыменуйте тип:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
{
MKMyPointAnnotation *myAnnotation = (MKMyPointAnnotation *)annotation;
object = [arrayResults objectAtIndex:myAnnotation.index]; // <-- your index property
switch ([[object iden] intValue])
{
case 1: // type one
.
.
.
}
.
.
.
}