绘制两种不同颜色的mapview引脚

时间:2013-03-13 10:25:34

标签: ios objective-c mkmapview

我正在尝试使用坐标数组显示mapview。所以,我从那些包含的数组加载纬度和经度。它显示完美。现在,我想显示一个圆形纬度和经度与不同颜色的针。我已经提到this answer没有我看不到的那种不同。

ViewController.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;
}

如果坐标在那里,我只是写条件我只能将绿色图钉绘制到该特定坐标。怎么做到这一点?

2 个答案:

答案 0 :(得分:1)

您的代码与Hinata之间的区别在于,其他代码中的if语句在其当前绘制的注释上使用值(yesno)来决定使用何种颜色。你从DataProvider得到一个值,但没有告诉它你正在绘制哪个注释,所以它给你的实例就是instance方法在地图要求时返回的感觉。销。你需要告诉它你正在绘制什么来决定放入colorlatitude

的内容

答案 1 :(得分: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;
}

在我的视图中,我收到了来自DataProvider的坐标,并分配了带有标记的单独MapviewAnnotations,并简单地将其区分为三种类型的针脚。

干杯!