简体   繁体   English

如何在x个移动注解之间画线?

[英]how to draw line between x number of moving annotations?

Hai am new to xcode am developing an iOS app for vehicle tracking using mkmap I need to draw lines between the annotations for every 5 seconds based on the vehicle moving, My prob is it draw the line for the first time only and from the second refresh interval it it won't works my code is below, 我是xcode的新手,正在开发一个使用mkmap进行车辆跟踪的iOS应用程序,我需要根据车辆的行驶情况每5秒在注释之间绘制一条线,我的问题是,这仅是第一次绘制,第二次刷新后才绘制间隔它不起作用我的代码在下面,

- (void)viewDidLoad
{
   [super viewDidLoad];
   aTimer = [NSTimer scheduledTimerWithTimeInterval:5
                                                      target:self
                                                    selector:@selector(timerFired:)
                                                    userInfo:nil
                                                     repeats:YES];
}
-(void)timerFired:(NSTimer *) theTimer
{
    NSArray *existingpoints = MapViewC.annotations;
    if ([existingpoints count])
        [MapViewC removeAnnotations:existingpoints];
    NSString *urlMapString=[NSString stringWithFormat:@"http://www.logix.com/logix_webservice/map.php?format=json&truckno=%@",nam2];
    NSURL *urlMap=[NSURL URLWithString:urlMapString];
    NSData *dataMap=[NSData dataWithContentsOfURL:urlMap];
    NSError *errorMap;
    NSDictionary *jsonMap = [NSJSONSerialization JSONObjectWithData:dataMap options:kNilOptions error:&errorMap]; NSArray *resultsMap = [jsonMap valueForKey:@"posts"];
    NSArray *resMap = [resultsMap valueForKey:@"post"];
    NSArray *latitudeString=[resMap valueForKey:@"latitude"];
    NSString *latOrgstring = [latitudeString objectAtIndex:0];
    latitude=[latOrgstring doubleValue];
    NSArray *longitudeString=[resMap valueForKey:@"longitude"];
    NSString *longOrgstring = [longitudeString objectAtIndex:0];
    longitude=[longOrgstring doubleValue];
    NSString *ignation=[[resMap valueForKey:@"ignition"]objectAtIndex:0];
    //MAP VIEW Point
    MKCoordinateRegion myRegion;
    //Center
    CLLocationCoordinate2D center;
    center.latitude=latitude;
    center.longitude=longitude;
    //Span
    MKCoordinateSpan span;
    span.latitudeDelta=0.01f;
    span.longitudeDelta=0.01f;
    myRegion.center=center;
    myRegion.span=span;
    //Set our mapView
    [MapViewC setRegion:myRegion animated:YES];
    //Annotation
    //1.create coordinate for use with the annotation
    //CLLocationCoordinate2D wimbLocation;
    wimbLocation1.latitude=latitude;
    wimbLocation1.longitude=longitude;
    Annotation * myAnnotation= [Annotation alloc];
    CLLocation *someLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
        addressOutlet=[dictionary valueForKey:@"Street"];
        City=[dictionary valueForKey:@"City"];
        State=[dictionary valueForKey:@"State"];
        myAnnotation.coordinate=wimbLocation1;
        if (addressOutlet!=NULL&&City!=NULL)
        {
            myAnnotation.title=addressOutlet;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@,%@", City, State];
        }
        [self.MapViewC addAnnotation:myAnnotation];
         [self line];
    }];
  }
  -(void)line
{
    CLLocationCoordinate2D coordinateArray[2];
    coordinateArray[0] = CLLocationCoordinate2DMake(latitude, longitude);
    coordinateArray[1] = CLLocationCoordinate2DMake(latitude, longitude);
    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
    [self.MapViewC addOverlay:self.routeLine];


}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    if(overlay == self.routeLine)
    {
        if(nil == self.routeLineView)
        {
            self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 5;
        }
        return self.routeLineView;
    }
    return nil;
}

Kindly advice me to correct my errors. 请建议我改正我的错误。 Thanks in advance... 提前致谢...

Try this.... this will help you... 试试这个...。这将帮助您...

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{

        {
            self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 5;
        }
        return self.routeLineView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM