2013-03-13 55 views
1

我试图用坐标数组显示mapview。所以,我加载阵列中包含的纬度和经度。它显示完美。现在,我想用不同的颜色引脚显示一个经纬度&。我已经提到this answer没有这样的不同,我看不到。绘制两种不同颜色的mapview针

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

我只写了条件,如果坐标存在,我只能将绿色针脚绘制到该特定坐标上。如何实现这一目标?

+0

你可以使用PIN图像 [annView setImageName:@“pin-green.png”]; – Pratik 2013-03-13 10:30:36

+0

@Pratik我试过了。它的工作。但是,我想将其展示为特定的纬度和经度的坐标。 – Praveenkumar 2013-03-13 10:33:43

+0

雅,但使用两个不同颜色的针脚一个红色和一个绿色,并在其他条件下放入 – Pratik 2013-03-13 10:38:17

回答

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,并简单地用三种类型的引脚进行区分。

干杯!

1

您的代码与Hinata提到的一个区别在于,其他代码中的if语句在其当前绘制的注释上使用值(yesno)来决定要使用的颜色。您从DataProvider获得一个值,但不会告诉它您正在绘制哪个注释,因此它提供给您的实例只是instance方法在映射要求引脚时返回的方式。你需要告诉它你画什么,以决定什么投入colorlatitude