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

Ios 如何在地图上的所有注释引脚上显示不同的图像?,ios,objective-c,mkmapview,mkannotationview,Ios,Objective C,Mkmapview,Mkannotationview,我有两个位置,一个起点和另一个终点,在这两个点上我想添加两个不同的管脚注释 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. } 这是我的代码,问题是我

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

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.
}
这是我的代码,问题是我在两个位置都得到了相同的图像

位置是 位置1为

-(MKAnnotationView*)地图视图:(MKMapView*)地图查看器视图用于注释:(id)注释{
静态NSString*mapIdentifier=@“mapIdentifier”;
MKAnnotationView*myAnnotation=[mapViewer dequeueReusableAnnotationViewWithIdentifier:mapIdentifier];
CLLocationCoordinate2D parkCllocation=CLLocationCoordinate2DMake([[u-tripDetails[@“park\u-lat”]doubleValue],[u-tripDetails[@“park\u-long”]doubleValue]);
MKPointAnnotation*jauntAnnotationPark=[[MKPointAnnotation alloc]init];
//jauntAnnotationPark.image=[UIImage ImageName:@“pinks.jpg”];
jauntAnnotationPark.coordinate=停车位置;
CLLocationCoordinate2D orginCllocation=CLLocationCoordinate2DMake([[u tripDetails[@“origin\u lat”]doubleValue],[u tripDetails[@“origin\u long”]doubleValue]);
MKPointAnnotation*jauntAnnotationOrgin=[[MKPointAnnotation alloc]init];
jauntAnnotationOrgin.coordination=orginlocation;
CLLocation*location=[[CLLocation alloc]INITWITHLATIONE:[[u tripDetails[@“origin\u lat”]doubleValue]经度:[[u tripDetails[@“origin\u long”]doubleValue];
CLLocation*location1=[[CLLocation alloc]INITWITHLATIONE:[[u tripDetails[@“park\u lat”]doubleValue]经度:[[u tripDetails[@“park\u long”]doubleValue]];
myAnnotation=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:mapIdentifier];
check=[[NSMutableArray alloc]initWithObjects:location,location1,nil];
for(NSString*检查中的位置)
{
int i;
myAnnotation.image=[UIImage ImageName:@”pin7@2x.png"];
}
如果(!myAnnotation){
myAnnotation=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:mapIdentifier];
}
否则{
注释=注释;
}
返回myAnnotation;

}

您的代码只设置了一个图像-pin7,并且您的代码还包含大量冗余;您将注释视图出列到myAnnotation中,然后alloc/init一个新的注释视图,然后检查它是否为nil,因为您只是alloc/init了一个注释视图,所以不会是nil

您的检查数组包含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*)地图视图:(MKMapView*)地图查看器视图用于注释:(id)注释{
静态NSString*mapIdentifier=@“mapIdentifier”;
MKAnnotationView*myAnnotation=[mapViewer dequeueReusableAnnotationViewWithIdentifier:mapIdentifier];
如果(myAnnotation==nil){
myAnnotation=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:mapIdentifier];
}
CLLocationCoordinate2D originCllocation=CLLocationCoordinate2DMake([[u tripDetails[@“origin\u lat”]doubleValue],[u tripDetails[@“origin\u long”]doubleValue]);
if(originCllocation.latitude==annotation.coordinate.latitude&&originCllocation.longitude==annotation.coordinate.longitude){
myAnnotation.image=[UIImage ImageName:@“startPin.png”];
}否则{
myAnnotation.image=[UIImage ImageName:@“endPin.png”];
}
返回myAnnotation;
}

您的代码只设置了一个图像-pin7,并且您的代码还包含大量冗余;您将注释视图出列到myAnnotation中,然后alloc/init一个新的注释视图,然后检查它是否为nil,因为您只是alloc/init了一个注释视图,所以不会是nil

您的检查数组包含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*)地图视图:(MKMapView*)地图查看器视图用于注释:(id)注释{
静态NSString*mapIdentifier=@“mapIdentifier”;
MKAnnotationView*myAnnotation=[mapViewer dequeueReusableAnnotationViewWithIdentifier:mapIdentifier];
如果(myAnnotation==nil){
myAnnotation=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:mapIdentifier];
}
CLLocationCoordinate2D originCllocation=CLLocationCoordinate2DMake([[u tripDetails[@“origin\u lat”]doubleValue],[u tripDetails[@“origin\u long”]doubleValue]);
if(originCllocation.latitude==annotation.coordinate.latitude&&originCllocation.longitude==annotation.coordinate.longitude){
myAnnotation.image=[UIImage ImageName:@“startPin.png”];
}否则{
myAnnotation.image=[UIImage ImageName:@“endPin.png”];
}
返回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"];
    }
-(MKAnnotationView*)地图视图:(MKMapView*)地图查看器视图用于注释:(id)注释
{
if(annotation.title.isEqualToString(@“Source”))
{
myAnnotation.image=[UIImage ImageName:@“startPin.png”];
}否则{
myAnnotation.image=[UIImage ImageName:@“endPin.png”];
}

是的,你也可以通过设置条件来实现

  -(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"];
    }
-(MKAnnotationView*)地图视图:(MKMapView*)地图查看器视图用于注释:(id)注释
{
if(annotation.title.isEqualToString(@“Source”))
{
myAnnotation.image=[UIImage ImageName:@“startPin.png”];
}否则{