2013-03-13 50 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

相關問題