views:

1330

answers:

3

Hi!Before anything i'm sorry for my bad english.

I'm trying to draw a route in a UIView and set that view as a MKMapView.

Now i have a UIView class (ViewClass) where i put all the touches methods and set the view of my viewcontroller as an instance of that class. The MKMapView is a viewcontroller.view subview. I can get all the touches in MKMapView. To draw a trail I set a UIView (trailView) as MKMapView subview with background color clear. I try to move the trailView when the MKMapView is moving but i'm not getting good results.The movement isn't perfect.

The idea is to have the view attached to the MKMapView. I don't know if it is possible or if i really have to use MKAnnotationView. I've tried before with MKAnnotationView but that consumes many memory.

I really appreciate some help. Thanks in advance.

A: 

This is a very good question.

How about you get the pixel data from the area of the map you are interested in and based on the color of the path, do nearest neighbor calculations. in the direction you are looking for. Save this path as you go, and render a different color into your trailView?

What also makes this more complicated is zoom, because you will have to have a resolution independent trail (convert a map coordinate) and not a pixel coordinate. So you convert your pixel coordinates to map coordinates and then it should scale for zoom.

David Sowsy
I didn't understand what you mean with "based on the color of the path, do nearest neighbor calculations". Aren't this way more complicated?I still have the dough if using MKAnnotationView is not a great idea. Does MKAnnotationView consumes much memory? I have a version of my app where i use this but it crash with low memory after i create some annotations.Thanks for your reply :)
ideafactory
Perhaps I misunderstood. When you say the movement isn't perfect, I think that you mean the trail isn't lined up. Did you mean the performance is unacceptable even though it is accurate?From the Apple docs: "Annotation objects do not provide the visual representation of the annotation but typically coordinate (in conjunction with the map view’s delegate) the creation of an appropriate MKAnnotationView object to handle the display."So if you are using lots of coordinates (thousands) in a small space MKAnnotationView doesn't seem to be right. Can you show us the code and the crash log?
David Sowsy
A: 

The trail is line up but when i scroll i get a bad result. In relation to the version of my app with MKAnnotatioView i can show you the code but i can't show you the log 'cause unfortunately i'm not with iphone now...

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

}

I don't know if you'll understand anything. My code is based on link text. Thanks for your help!! :)

ideafactory
+1  A: 

iOS4 has a new overlay system, including the rendering of paths. I haven't used it yet, but it seems like that'd be WAY simpler than trying to roll your own overlay.

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

Dan Ray