使用UIView作为MKMapView子视图绘制路线

时间:2010-01-14 17:50:22

标签: iphone uiview mkmapview draw trail

在此之前我很抱歉我的英语不好。

我正在尝试在UIView中绘制路线并将该视图设置为MKMapView。

现在我有一个UIView类(ViewClass),我把所有的touches方法放在一起,并将viewcontroller的视图设置为该类的一个实例。 MKMapView是一个viewcontroller.view子视图。 我可以在MKMapView中获得所有的功能。 为了绘制轨迹,我将UIView(trailView)设置为MKMapView子视图,背景颜色为clear。 当MKMapView移动时我尝试移动trailView但是我没有得到好的结果。运动并不完美。

想法是将视图附加到MKMapView。我不知道是否可能或者我是否真的必须使用MKAnnotationView。 我之前尝试过使用MKAnnotationView,但这会消耗很多内存。

我真的很感激一些帮助。 提前谢谢。

3 个答案:

答案 0 :(得分:1)

iOS4有一个新的叠加系统,包括路径渲染。我还没有使用它,但似乎比试图滚动自己的叠加层更简单。

请参阅http://developer.apple.com/iphone/library/documentation/MapKit/Reference/MKOverlayPathView_class/Reference/Reference.html#//apple_ref/doc/uid/TP40009713

答案 1 :(得分:0)

这是一个非常好的问题。

如何从您感兴趣的地图区域获取像素数据,并根据路径的颜色,进行最近邻居计算。在你正在寻找的方向。保存此路径,并在trailView中呈现不同的颜色?

更复杂的是缩放,因为你必须有一个独立于分辨率的轨迹(转换地图坐标)而不是像素坐标。因此,您将像素坐标转换为地图坐标,然后它应缩放以进行缩放。

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

}

我不知道你是否理解任何事情。我的代码基于link text。 谢谢你的帮助!! :)