1

我正在尝试显示带有坐标数组的地图视图。所以,我从那些包含的数组中加载纬度和经度。它完美地显示出来。现在,我想用不同的颜色针显示一个特定的纬度和经度。我已经提到了这个答案,我看不出有什么不同。

视图控制器.m


-(void)showMap:
{
...
...
DataProvider *d = [DataProvider getInstance];
NSInteger numb = sender.view.tag;
d.colorlatitude = [[arraxy objectAtIndex:numb] objectForKey:@"lat"];
d.colorlongitude = [[arraxy objectAtIndex:numb] objectForKey:@"lng"];
[d._address removeAllObjects];
[arraxy removeObjectAtIndex:sender.view.tag];
d._address = arraxy;

MapViewController *map = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:nil];
[self.navigationController pushViewController:map animated:YES];

}

MapViewController.m


-(void)viewWillAppear:(BOOL)animated
{
DataProvider *d = [DataProvider getInstance];
[_mapView removeAnnotations:_mapView.annotations];

RegisterViewController *appDelegate = [[RegisterViewController alloc]init];

[_mapView setShowsUserLocation:YES];
[_mapView setRegion:MKCoordinateRegionMakeWithDistance([appDelegate.locationManager location].coordinate, 1000, 1000)];
[_mapView setUserTrackingMode:MKUserTrackingModeNone];

MKCoordinateRegion rregion = {{0.0,0.0},{0.0,0.0}};
rregion.center.latitude = [d.colorlatitude floatValue];
rregion.center.longitude = [d.colorlongitude floatValue];
rregion.span.latitudeDelta=0.001f;
rregion.span.longitudeDelta=0.001f;
[_mapView setRegion:rregion];

MapviewAnnotations *add = [[MapviewAnnotations alloc]init];
add.coordinate = rregion.center;
[_mapView addAnnotation:add];


if (d._address)
{
    for (int i=0; i<[d._address count]; i++)
    {
        NSDictionary *dic=[d._address objectAtIndex:i];
        MKCoordinateRegion region={{0.0,0.0},{0.0,0.0}};
        region.center.latitude=[[dic objectForKey:@"lat"]floatValue];
        region.center.longitude=[[dic objectForKey:@"lng"]floatValue];
        region.span.latitudeDelta=0.001f;
        region.span.longitudeDelta=0.001f;
        [_mapView setRegion:region];

        MapviewAnnotations *ann=[[MapviewAnnotations alloc]init];
        ann.coordinate=region.center;
        [_mapView addAnnotation:ann];
    }
}
[super viewWillAppear:YES];
}

而我MKMapView的委托方法是

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id  <MKAnnotation>)annotation
{
if (![annotation isKindOfClass:[MapviewAnnotations class]])
{
    return nil;
}

static NSString *reuseId = @"currentloc";

MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annView == nil)
{
    annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
    annView.animatesDrop = NO;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
}
else
{
    annView.annotation = annotation;
}

DataProvider *mvAnn = [DataProvider getInstance];
if (mvAnn.colorlatitude) // here i'm checking the condition.
{
    annView.pinColor = MKPinAnnotationColorGreen;
}
else
{
    annView.pinColor = MKPinAnnotationColorRed;
}

return annView;
}

如果坐标在那里,我只是在写条件,我只需要将绿色引脚绘制到那个特定的坐标。如何做到这一点?

4

2 回答 2

1

您的代码与 Hinata 提到的代码之间的区别在于,其他代码中的 if 语句在yesno它当前绘制的注释上使用了一个值 ( ) 来决定使用什么颜色。您正在从中获取一个值,DataProvider但没有告诉它您正在绘制哪个注释,因此它为您提供的实例正是该instance方法在地图请求引脚时返回的感觉。你需要告诉它你在画什么,让它决定放什么colorlatitude

于 2013-03-14T00:03:49.743 回答
0

我已经通过下面的一些标志来做到这一点- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

MapviewAnnotations *mvAnn = (MapviewAnnotations *)annotation;
if (mvAnn.flag == 1)
{
    annView.pinColor = MKPinAnnotationColorGreen;
}
else if (mvAnn.flag == 10)
{
    annView.pinColor = MKPinAnnotationColorPurple;
}
else
{
    annView.pinColor = MKPinAnnotationColorRed;
}

在我的 viewWillAppear 中,我收到了坐标DataProvider并分配了单独MapviewAnnotations的标志,并简单地用三种类型的引脚区分它。

干杯!

于 2013-03-20T07:30:22.270 回答