2010-01-14 42 views
3

在我爲我的壞英語感到抱歉之前。使用UIView作爲MKMapView子視圖來繪製路線

我試圖在UIView中繪製路線並將該視圖設置爲MKMapView。

現在我有一個UIView類(ViewClass),我把所有的觸摸方法,並將我的viewcontroller的視圖設置爲該類的一個實例。 MKMapView是一個viewcontroller.view子視圖。 我可以在MKMapView中獲得所有的接觸。 要繪製軌跡,我將UIView(trailView)設置爲背景顏色清晰的MKMapView子視圖。 我試圖在MKMapView移動時移動trailView,但我沒有得到好的結果。運動並不完美。

這個想法是將視圖附加到MKMapView。我不知道是否有可能,或者如果我真的必須使用MKAnnotationView。 我以前用MKAnnotationView試過,但是消耗了很多內存。

我真的很感謝一些幫助。 在此先感謝。

回答

0

這是一個很好的問題。

您如何從您感興趣的地圖區域獲取像素數據並根據路徑的顏色進行最近鄰居計算。在你正在尋找的方向。保存這個路徑,並在你的trailView中渲染一個不同的顏色?

什麼讓這更復雜的縮放,因爲你將不得不有一個解決方案獨立軌跡(轉換地圖座標)而不是像素座標。所以你把你的像素座標轉換成地圖座標,然後它應該縮放縮放。

+0

我不明白你的意思是什麼「基於路徑的顏色,做近鄰計算」。這不是更復雜嗎? 我仍然有面團,如果使用MKAnnotationView不是一個好主意。 MKAnnotationView是否消耗大量內存?我有我的應用程序的版本,我使用這個,但它創建一些註釋後,與低內存崩潰。 感謝您的回覆:) – ideafactory 2010-01-21 12:05:55

+0

也許我誤解了。當你說運動不是完美的時候,我認爲你的意思是這條線沒有排成隊。即使它是準確的,你的意思是表現是不可接受的嗎? 來自Apple文檔: 「註釋對象不提供註釋的可視化表示,但通常會協調(連同地圖視圖的委託)創建適當的MKAnnotationView對象來處理顯示。」 所以如果你在一個小空間中使用很多座標(千)MKAnnotationView似乎並不正確。你能告訴我們代碼和崩潰日誌嗎? – 2010-01-21 14:07:44

0

線索是排隊,但當我滾動我得到一個壞結果。 關於我與MKAnnotatioView應用程序的版本,我可以告訴你的代碼,但我不能告訴你的日誌「引起不幸的是我不是現在iphone ...

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

self.startingPoint = newLocation; 

CLLocation *lastLocation; 

if([pointArray count] >= 2) 
{ 
    lastLocation = [pointArray objectAtIndex:1]; 
    [pointArray removeAllObjects]; 
}  

mapView.showsUserLocation = NO; 

double lat,lon;  
lat = newLocation.coordinate.latitude; 
lon = newLocation.coordinate.longitude;   

if(distanceIndex == 0) 
{ 
    [pointArray insertObject:newLocation atIndex:0]; 
} 
else 
{    
    if(distanceIndex == 1) 
    { 
     [pointArray insertObject:newLocation atIndex:1]; 
    } 
    else 
    { 
     [pointArray insertObject:lastLocation atIndex:0]; 
     [pointArray insertObject:newLocation atIndex:1]; 
    } 
}   

if(distanceIndex == 0) 
{ 
    MarkPlace *mark = nil; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
    [dateFormatter setDateStyle:NSDateFormatterNoStyle]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];  
    NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]]; 
    [dateFormatter release]; 
    NSString *subtitle = [NSString stringWithFormat:@"Time Here: %@", dateStr]; 
    mark = [[[MarkPlace alloc] initWithCoordinate:[[pointArray objectAtIndex:0] coordinate] currentTime:subtitle annotationType:MarkPlaceTypeEnd title:@"Pin Title"] autorelease]; 
    [mapView addAnnotation:mark]; 




} 

//create the route 
MKCoordinateSpan span = mapView.region.span; 
CLLocationCoordinate2D center = mapView.region.center; 

IFRouteAnnotations *routeAnnotation = [[[IFRouteAnnotations alloc] initWithPoints:pointArray:0:span:center] autorelease]; 
[mapView addAnnotation:routeAnnotation]; 

[mapView setRegion:routeAnnotation.region animated:TRUE]; 

distanceIndex = distanceIndex + 1; 
[routeAnnotation release]; 

}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { 
//re-enable and re-position the route display 
for (NSObject *key in [routeViews allKeys]) { 
    IFRouteView *routeView = [routeViews objectForKey:key]; 
    [routeView regionChanged]; 
} 

}

-(MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation{ 

MKAnnotationView *annotationView = nil; 
NSLog(@"viewForAnnotation"); 
if(distanceIndex == 0) 
{  
    MKPinAnnotationView *pin=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease]; 
    pin.pinColor = MKPinAnnotationColorGreen; 
    pin.animatesDrop = TRUE; 
    pin.userInteractionEnabled = YES; 

    annotationView = pin; 
    [annotationView setEnabled:YES]; 
    [annotationView setCanShowCallout:YES]; 
} 

if([annotation isKindOfClass:[IFRouteAnnotations class]]) 
{ 
    IFRouteAnnotations *routeAnnotation = (IFRouteAnnotations *)annotation; 

    annotationView = [routeViews objectForKey:routeAnnotation.routeID]; 

    if(annotationView == nil) 
    { 
     IFRouteView *routeView = [[[IFRouteView alloc] initWithFrame:CGRectMake(0,0, mapView.frame.size.width, mapView.frame.size.height)]autorelease]; 

     routeView.annotation = routeAnnotation; 
     routeView.mapView = mapView; 

     [routeViews setObject:routeView forKey:routeAnnotation.routeID]; 

     annotationView = routeView; 
    } 
} 
return annotationView; 

}

我不知道你會明白的ything。我的代碼基於link text。 感謝您的幫助! :)