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

时间:2016-02-25 05:53:49

标签: ios mkmapview

我有一个有4-5个位置的字典,虽然它可以变化。需要在地图视图上显示所有这些位置的不同图像。在字典中有一个起始位置和其他位置。我已经创建了注释起点和终点。但并非所有位置都在地图上看到。以下是我试过的代码。

-(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中我存储了lat n的位置,然后将它添加到cllocationcoordinate2D,当我在viewforannotation中添加这个cllocationcoordinate2D时,从不进入内部

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

条件。

Plz帮我解决了这个问题。

1 个答案:

答案 0 :(得分:0)

您可以将以下代码用于多个引脚。

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)
}