【问题标题】:how to show multiple locations with custom annotation in map view如何在地图视图中使用自定义注释显示多个位置
【发布时间】:2016-02-25 05:53:49
【问题描述】:

我有一本有 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 long,然后将其添加到 cllocationcoordinate2D,当我在 viewforannotation 中添加这个 cllocationcoordinate2D 时,永远不会进入

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

条件。

请帮帮我,我陷入了困境。

【问题讨论】:

    标签: ios mkmapview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多