如何在地图视图中显示带有自定义注释的多个位置

how to show multiple locations with custom annotation in map view

我有一本包含 4-5 个位置的字典,虽然它可以 vary.In 需要在地图视图中显示所有这些位置,但 images.There 是起始位置,其他位置在 dictionary.I 已经为起点和终点创建了注释 point.But 并非所有位置都在地图上可见。下面是我试过的代码。

-(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 parkCllocation=CLLocationCoordinate2DMake([_tripDetails[@"park_lat"] doubleValue], [_tripDetails[@"park_long"] doubleValue]);

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

 CLLocationCoordinate2D location=CLLocationCoordinate2DMake([loc[@"lat"] doubleValue], [loc[@"long"] doubleValue]);

if ((annotation.coordinate.latitude == parkCllocation.latitude)
               && (annotation.coordinate.longitude == parkCllocation.longitude))
{
    myAnnotation.image=[UIImage imageNamed:@"pin1@2x.png"];
}

if ((annotation.coordinate.latitude == orginCllocation.latitude)
    && (annotation.coordinate.longitude == orginCllocation.longitude))
{
    myAnnotation.image=[UIImage imageNamed:@"pin7@2x.png"];
}


if ((annotation.coordinate.latitude == location.latitude)
    && (annotation.coordinate.longitude == location.longitude))
{
    myAnnotation.image=[UIImage imageNamed:@"pin4@2x.png"];
}
return myAnnotation
}

这是我获取位置的代码,在 loc dict 中,我存储了位置的经纬度,然后将其添加到 clocationcoordinate2D,当我在 viewforannotation 中添加这个 clocationcoordinate2D 时,永远不会进入

if ((annotation.coordinate.latitude == location.latitude)
        && (annotation.coordinate.longitude == location.longitude)) 

条件。

请帮我解决这个问题。

您可以对多个引脚使用以下代码。

func dropPins(){
    var i = Int()
    var coord = CLLocationCoordinate2D()
    for i = 0 ; i < self.workLocationArray.count ; i++ {
        let lat = NSNumber(float:(self.workLocationArray.objectAtIndex(i).valueForKey("lat")!.floatValue)) as CLLocationDegrees
        let long = NSNumber(float:(self.workLocationArray.objectAtIndex(i).valueForKey("lng")!.floatValue)) as CLLocationDegrees
        let location = self.workLocationArray.objectAtIndex(i).valueForKey("client") as! String
        let title = self.workLocationArray.objectAtIndex(i).valueForKey("job") as! String
        coord.latitude = lat
        coord.longitude = long
        let locationq:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
        let annotation = CustomPointAnnotation()
        annotation.coordinate = locationq
        annotation.title = title
        annotation.subtitle = location
        annotation.imageName = "ic_location"
        self.mapView.addAnnotation(annotation)
    }
    var region = MKCoordinateRegion()
    region.center=coord;
    region.span.longitudeDelta=1 ;
    region.span.latitudeDelta=1;
    self.mapView.setRegion(region, animated: true)
}