【问题标题】:how to draw line between x number of moving annotations?如何在 x 个移动注释之间画线?
【发布时间】:2014-07-21 10:21:55
【问题描述】:

我是 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;
}

请建议我纠正我的错误。提前谢谢...

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example。
  • 您只需在方法 -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:( id)覆盖{
  • @Spynet 非常感谢你,现在它可以完美运行了......
  • @iBeginner 总是欢迎如果答案真的有助于解决然后只接受
  • @Spynet 先生,不客气..

标签: ios mkmapview mkoverlay


【解决方案1】:

试试这个....这会帮助你...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-23
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    相关资源
    最近更新 更多