Using UIView as a subclass of MKMapView to draw a route

Before anything, I apologize for my poor English.

I am trying to draw a route in a UIView and set this view as MKMapView.

Now I have a UIView class (ViewClass), where I put all the touch methods and set the view of my view manager as an instance of this class. MKMapView is a viewcontroller.view view. I can get all touches in MKMapView. To draw a trail, I set UIView (trailView) as a subclass of MKMapView with a clear background color. I am trying to move trailView when MKMapView is moving, but I am not getting good results. The movement is not perfect.

The idea is for the view to be attached to MKMapView. I don't know if this is possible, or I really need to use MKAnnotationView. I tried before with MKAnnotationView, but it takes a lot of memory.

I really appreciate some help. Thanks in advance.

+3
source share
3 answers

iOS4 has a new overlay system, including path visualization. I haven't used it yet, but it seems like it would be easier than trying to flip my own overlay.

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

+1
source

This is a very good question.

, . , . trailView?

, ( ), . , , .

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;

}

, -. . !!:)

0

Source: https://habr.com/ru/post/1728497/


All Articles