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

时间:2016-02-24 10:04:27

标签: ios objective-c mkmapview mkannotationview

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

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

位置< + 47.45025000,-122.30881700> 和location1是< + 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;

}

3 个答案:

答案 0 :(得分:3)

您的代码只设置一个图像 - pin7,您的代码也包含大量冗余;你将一个注释视图出列到myAnnotation然后分配/初始化一个新的,然后你检查它是否是nil它不会是因为你只是分配/ 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;
}

答案 1 :(得分:1)

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

并在视图中实现注释检查

public void saveRadioButtons(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("Gender", radioSexButton.isChecked());
    editor.putBoolean("Male", rdoMale.isChecked());
    editor.apply();
}

答案 2 :(得分:0)

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

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