3

在任何事情之前,我很抱歉我的英语不好。

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

现在我有一个 UIView 类 (ViewClass),我在其中放置了所有的触摸方法,并将我的 viewcontroller 的视图设置为该类的一个实例。MKMapView 是一个 viewcontroller.view 子视图。我可以在 MKMapView 中进行所有操作。为了绘制轨迹,我将 UIView (trailView) 设置为背景颜色清晰的 MKMapView 子视图。当 MKMapView 移动时,我尝试移动 trailView,但效果不佳。移动并不完美。

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

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

4

3 回答 3

1

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

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

于 2010-06-25T13:34:11.320 回答
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;

}

我不知道你会不会明白什么。我的代码基于链接文本。谢谢你的帮助!!:)

于 2010-01-21T15:59:20.660 回答
0

这个问题问得好。

您如何从您感兴趣的地图区域获取像素数据,并根据路径的颜色,进行最近邻计算。在你正在寻找的方向。边走边保存这条路径,然后在 TrailView 中渲染不同的颜色?

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

于 2010-01-20T18:40:23.380 回答