如何在地图上的所有注释图钉上显示不同的图像?

How to show different image on all the annotation pins on map?

我有两个位置,一个起点和一个终点,我想在这两个点上添加两个不同的引脚注释

这是我的代码,问题是我在两个位置都得到了相同的图像

位置 <+47.45025000,-122.30881700> 位置 1 是 <+47.62212000,-122.35410000>

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

    static NSString *mapIdentifier=@"mapIdentifier";

    MKAnnotationView *myAnnotation=[mapViewer dequeueReusableAnnotationViewWithIdentifier:mapIdentifier];
    CLLocationCoordinate2D parkCllocation=CLLocationCoordinate2DMake([_tripDetails[@"park_lat"] doubleValue], [_tripDetails[@"park_long"] doubleValue]);

    MKPointAnnotation *jauntAnnotationPark =[[MKPointAnnotation alloc] init];
    // jauntAnnotationPark.image = [UIImage imageNamed:@"pinks.jpg"];
    jauntAnnotationPark.coordinate=parkCllocation;

    CLLocationCoordinate2D orginCllocation=CLLocationCoordinate2DMake([_tripDetails[@"origin_lat"] doubleValue], [_tripDetails[@"origin_long"] doubleValue]);

    MKPointAnnotation *jauntAnnotationOrgin =[[MKPointAnnotation alloc] init];
    jauntAnnotationOrgin.coordinate=orginCllocation;
    CLLocation *location = [[CLLocation alloc] initWithLatitude:[_tripDetails[@"origin_lat"] doubleValue] longitude:[_tripDetails[@"origin_long"] doubleValue]];

    CLLocation *location1 = [[CLLocation alloc] initWithLatitude:[_tripDetails[@"park_lat"] doubleValue] longitude:[_tripDetails[@"park_long"] doubleValue]];

    myAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:mapIdentifier];

    check=[[NSMutableArray alloc]initWithObjects:location,location1, nil];

    for (NSString *location in check)
    {

        int i;            
        myAnnotation.image=[UIImage imageNamed:@"pin7@2x.png"];

    }

    if (!myAnnotation) {

        myAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:mapIdentifier];

}
    else {
        myAnnotation.annotation=annotation;
    }

    return myAnnotation;

}

你的代码只设置了一张图片——pin7,而且你的代码也有很多冗余;您将注释视图出列到 myAnnotation 中,然后 alloc/init 一个新的,然后您检查它是否为 nil 而不是,因为您只是 alloc/inited 一个。

您的检查数组包含 CLLocation 对象,但您将该数组迭代为一个 NSString 引用 - 然后无论如何都不对该值执行任何操作。

您可以使用此代码出列视图,如果不能出列则分配一个新视图,然后设置适当的图像;此代码假定只有两个注释,如果它不是起点,那么它一定是终点。如果有不止这两个注释,那么您需要修改代码来解决这个问题。

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

    static NSString *mapIdentifier=@"mapIdentifier";

    MKAnnotationView *myAnnotation=[mapViewer dequeueReusableAnnotationViewWithIdentifier:mapIdentifier];

    if (myAnnotation == nil) {
        myAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:mapIdentifier];
    }


    CLLocationCoordinate2D originCllocation=CLLocationCoordinate2DMake([_tripDetails[@"origin_lat"] doubleValue], [_tripDetails[@"origin_long"] doubleValue]);

    if (originCllocation.latitude==annotation.coordinate.latitude && originCllocation.longitude==annotation.coordinate.longitude) {
       myAnnotation.image=[UIImage imageNamed:@"startPin.png"];
    } else {
       myAnnotation.image=[UIImage imageNamed:@"endPin.png"];
    }

    return myAnnotation;
}

是的,你也可以通过设置条件来做到这一点。

  -(MKAnnotationView *)mapView:(MKMapView *)mapViewer viewForAnnotation:(id <MKAnnotation>)annotation
 {
    if (annotation.title.isEqualToString(@"Source"))
    {
        myAnnotation.image=[UIImage imageNamed:@"startPin.png"];
       } else {
        myAnnotation.image=[UIImage imageNamed:@"endPin.png"];
    }

遍历注释坐标的纬度和经度值。

并针对注解实施检查

for(int i=0; i <latArray.count; i++)
{

   //annotation.coordinate.latitude
   //annotation.coordinate.longitude
   //compare your latitude and longitude values and change the image accordingly.
}