1

iPhone の SQLite に保存されている座標に基づいて、MKMapView にパスを描画します。
しかし、今では 14000 の座標 (緯度/経度のみ) をデータベースに保存しましたが、オーバーレイ パスを表示しようとすると、アプリケーションがクラッシュします。
私の質問は、このコードを最適化して高速化する方法はありますか? これはロードされたビューです:

// ar is NSMutableArray and it is populate from database for a few seconds but code bellow cost me app crash
    for(Path* p in ar)
        {
            self.routeLine = nil;
            self.routeLineView = nil;

            // while we create the route points, we will also be calculating the bounding box of our route
            // so we can easily zoom in on it.
            MKMapPoint northEastPoint;
            MKMapPoint southWestPoint;

            // create a c array of points.
            MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * ar.count);

            for(int idx = 0; idx < ar.count; idx++)
            {
                Path *m_p = [ar objectAtIndex:idx];

                CLLocationDegrees latitude  = m_p.Latitude;
                CLLocationDegrees longitude = m_p.Longitude;

                // create our coordinate and add it to the correct spot in the array
                CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);


                MKMapPoint point = MKMapPointForCoordinate(coordinate);

                // adjust the bounding box
                // if it is the first point, just use them, since we have nothing to compare to yet.
                if (idx == 0) {
                    northEastPoint = point;
                    southWestPoint = point;
                }
                else
                {
                    if (point.x > northEastPoint.x)
                        northEastPoint.x = point.x;
                    if(point.y > northEastPoint.y)
                        northEastPoint.y = point.y;
                    if (point.x < southWestPoint.x)
                        southWestPoint.x = point.x;
                    if (point.y < southWestPoint.y)
                        southWestPoint.y = point.y;
                }

                pointArr[idx] = point;
            }

            // create the polyline based on the array of points.
            self.routeLine = [MKPolyline polylineWithPoints:pointArr count:ar.count];

            _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
            // clear the memory allocated earlier for the points
            free(pointArr);


            [self.mapView removeOverlays: self.mapView.overlays];
            // add the overlay to the map
            if (nil != self.routeLine) {
                [self.mapView addOverlay:self.routeLine];
            }



更新

ViewDidLoad:

...
[self performSelectorInBackground:@selector(drawPathInBackground) withObject:nil];
...
-(void)drawPathInBackground{
for(int idx = 0; idx < ar.count; idx++)
    { ... }
[self.mapView performSelector:@selector(addOverlay:) onThread:[NSThread mainThread] withObject:self.routeLine waitUntilDone:YES];
}

私はこれが好きで、UIはフリーズしません。
残された唯一のことは、すべての X ポイントにMKPolyLineを描画する方法ですか?

4

4 に答える 4

4

データベースからのフェッチとバックグラウンド スレッドでの処理を行います。

次に、 Douglas-Peucker アルゴリズムを使用してパス内の座標の数を減らします。

Douglas-Peucker アルゴリズム

そして結果をキャッシュします。

于 2013-08-26T12:49:20.923 に答える
1

座標の配列がある場合は、このコードを使用します

ここで routes は座標の配列です。

NSLog(@"count %d",[routes count]);
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routes count]);

for(int idx = 0; idx < [routes count]; idx++)
{
    CLLocation* location = [routes objectAtIndex:idx];
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=location.coordinate.latitude;
    workingCoordinate.longitude=location.coordinate.longitude;  
    NSLog(@"loop = %f,%f",workingCoordinate.latitude, workingCoordinate.longitude);
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[idx] = point;      
}   
// create the polyline based on the array of points. 
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[routes count]];
[mapView addOverlay:self.routeLine];
free(pointArr);

お役に立てれば。

于 2013-08-26T12:38:16.787 に答える
0

Google には、場所を文字列にエンコードできるアルゴリズムがあります。あなたの状況では、14000の座標がほぼ14000の長さの文字列にエンコードされます。次に、文字列を sqlite に入れます。DBからデータを取得する速度を加速します

于 2013-08-27T01:25:40.820 に答える