0

座標データを使ったアプリを作っています。私のアプリが起動し、この段階に到達する前に、別のビュー コントローラーを表示する必要があります。ユーザーは、この特定の に入る前に座標ルートを選択しますUIViewController

私は現在、テスト目的のために、これらすべてを で行っていviewDidLoadます。このView Controllerの目的は、MKPolyline事前に決められたルートに基づいて地図を表示することです。はMKPolyline、係数 (この場合は ) に基づくグラデーションになりますvelocity

GradientPolylineOverlayhttps://github.com/wdanxna/GradientPolyline、およびMapKit ios を使用したグラデーションポリラインからのものです。

私の主な問題は、私のコードをそのまま使用すると、 のレンダリングMKPolylineが非常に遅くなり、 のイベントmapViewが遅くて遅延することです。

多くの座標があるため (これは簡単に 7000 を超える可能性があります)、次の 2 つの行のいずれかが問題を引き起こしていると判断しました。 [mapView addOverlay:polyline]; [self.view addSubview:mapView];

- (void)viewDidLoad
{
    [super viewDidLoad];
    data = delegate.data; // contains sorted/parse data from my source
    NSInteger pointsCount = [data sampleCount];

    // set up mapView
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.delegate = self;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(*[data getGPSCoordinatesForSample:0], 500, 500);
    [mapView setRegion:region];

    CLLocationCoordinate2D pointsToUse[pointsCount];

    float* velocity =0;
    velocity = malloc(sizeof(float)*pointsCount);

    // pointCount contains 7000 coordinates!
    for(int i = 0; i < pointsCount; i++) {
            CGPoint p = CGPointMake([data getGPSCoordinatesForSample:i]->latitude,[data getGPSCoordinatesForSample:i]->longitude);
            pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
            velocity[i] =[data getGPSVelocityForSample:i];
    }


    polyline = [[GradientPolylineOverlay alloc] initWithPoints:pointsToUse velocity:velocity count:pointsCount];

    // issues!
    [mapView addOverlay:polyline];
    [self.view addSubview:mapView];
    free(velocity);
}

この時点で、パフォーマンスを向上させるには、Grand Central Dispatch を使用する必要があると思われます。GCD を使用して、コードを次のように変更しました。

dispatch_queue_t queue = dispatch_queue_create("Test", 0);
dispatch_async(queue, ^{

    CLLocationCoordinate2D pointsToUse[pointsCount];


    float* velocity =0;
    velocity = malloc(sizeof(float)*pointsCount);

    // pointCount contains 7000 coordinates!
    for(int i = 0; i < pointsCount; i++) {
        CGPoint p = CGPointMake([data getGPSCoordinatesForSample:i]->latitude,[data getGPSCoordinatesForSample:i]->longitude);
        pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
        velocity[i] =[data getGPSVelocityForSample:i];
    }


    polyline = [[GradientPolylineOverlay alloc] initWithPoints:pointsToUse velocity:velocity count:j];
    [mapView addOverlay:polyline]; // Crash
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view addSubview:mapView];
    });
});

[mapView addOverlay:polyline];Thread 4: EXC_BAD_ACCESSデバッグエラーを出します。

私の理想的な解決策は、すべての座標を使用する必要がありますが、必要に応じてこれを縮小できます。コードのある場所で GCD を使用できる場合、最適な場所はどこでしょうか?

ありがとうございました!

4

1 に答える 1

0

mapView:rendererForOverlayまたはmapViewDidFinishLoadingMapが良い選択肢だと思います。

どちらも MKMapViewDelegate にあります。

これが文書です。

于 2014-03-13T04:24:25.183 に答える