4

MKMapView に複数のオーバーレイを追加する方法を理解するのに苦労しています。

アレイルートを追跡しています。追跡されるルートは、CLLocations の配列です。だから私は配列の配列を持っています。

1 つのルートを取り、マップ ビューに描画することができます。ただし、マップ ビューにすべてのルートを描画する必要があります。

したがって、1 つのルートの MKPolyline を作成する方法は次のとおりです。

-(void) loadRoute
{
    //So we grab an array that holds all the locations of one route. 
            NSArray *locations = [[NSArray alloc] initWithArray:[routesArray objectAtIndex:0]];

            // 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) *[locations count]);

            for(int idx = 0; idx < [locations count]; idx++)
            {
                CLLocation *tempLoc = (CLLocation*)[locations objectAtIndex:idx];

                CLLocationDegrees latitude  = tempLoc.coordinate.latitude;
                CLLocationDegrees longitude = tempLoc.coordinate.longitude;

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

                MKMapPoint point = MKMapPointForCoordinate(coordinate);

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


            self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[locations count]];

//Zoom in to fit route on screen
            _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);

            // clear the memory allocated earlier for the points
            free(pointArr);

}

オーバーレイを返す MapKit デリゲート呼び出しは次のとおりです。

#pragma mark MKMapViewDelegate

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayView* overlayView = nil;

    if(overlay == self.routeLine)
    {
        //if we have not yet created an overlay view for this overlay, create it now. 
        if(nil == self.routeLineView)
        {
            self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 3;
        }
        overlayView = self.routeLineView;
    }

    return overlayView;
}

したがって、上記のコードはすべて正常に機能します。他のルートを表示するためにオーバーレイをさらに読み込む方法がわかりません。

おそらく loadRoute メソッドで、すべてのルートを実行し、それぞれに MKOverlayView を作成すると思いました。これらすべての MKOverlayView を配列に格納してから、次のようにします。 [self.mapView addOverlays:array];

しかし、うまくいきません。問題は、デリゲート メソッドで、どのオーバーレイを返すかを何らかの方法で知る必要があることです。

したがって、次の行に沿って何かを行うことができれば:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    int tag = overlay.tag;
    return (MKOverlayView*)[arrayOfViews objectAtIndex:tag];
}

それはうまくいくでしょう。しかし、残念ながらそのようには機能しません:(

どんな助けでも大歓迎です!

編集:

これを ViewDidLoad で使用して、オーバーレイを追加します。

[self loadRoute];

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

        // zoom in on the route. 
        [self zoomInOnRoute];

これはヘッダーにあります:

// the data representing the route points. 
    MKPolyline* _routeLine;

    // the view we create for the line on the map
    MKPolylineView* _routeLineView;

    // the rect that bounds the loaded points
    MKMapRect _routeRect;
4

1 に答える 1

4

routeLineおよび変数はrouteLineView、一度に1つのオーバーレイおよびオーバーレイビューのみを指すことができるため、理論的には、そのような変数の配列が必要になります。

ただし、示されているコードに基づいて、そもそもこれらへの参照を保持する必要はありません。

routeLinerouteLineView変数を削除することをお勧めします。

次に、オーバーレイオブジェクトをローカルで作成viewForOverlayし、要求されたoverlayパラメータのビューを返します。

このメソッドでは、ルートごとにとをloadRouteループし、ローカルオーバーレイオブジェクトを作成して呼び出すことができます。routesArrayaddOverlay

また、すべてのルートを境界とするマップrectを作成する簡単な方法があるため、マップのリージョンを設定してすべてのルートを表示できます。

例:

-(void) loadRoute
{
    _routeRect = MKMapRectNull;

    for (int raIndex = 0; raIndex < routesArray.count; raIndex++) 
    {
        //So we grab an array that holds all the locations of one route. 
        NSArray *locations = [[NSArray alloc] initWithArray:
                                 [routesArray objectAtIndex:raIndex]];
        //note we are getting object at raIndex (not 0) above

        //...no change to existing code here...

        //self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[locations count]];
        //replace above line with this...
        MKPolyline *pl = [MKPolyline polylineWithPoints:pointArr count:[locations count]];
        [mapView addOverlay:pl];

        //Zoom in to fit route on screen
        //_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
        //replace above line with this to create a rect around ALL routes...
        if (MKMapRectIsNull(_routeRect))
            _routeRect = pl.boundingMapRect;
        else
            _routeRect = MKMapRectUnion(_routeRect, pl.boundingMapRect);

        // clear the memory allocated earlier for the points
        free(pointArr);
    }    
}

ではviewDidLoad、を呼び出すだけでloadRoute、は呼び出さないでくださいaddOverlay


このviewForOverlay方法ははるかに簡単になります。

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *pv = [[MKPolylineView alloc] initWithPolyline:overlay];
    pv.fillColor = [UIColor redColor];
    pv.strokeColor = [UIColor redColor];
    pv.lineWidth = 3;
    return pv;
}


ちなみに、でloadRoute、この行:

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) *[locations count]);

する必要があります:

MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) *[locations count]);

MKMapPoint(ではなく)のサイズを使用する必要がありますCLLocationCoordinate2D
それらは同じものではありません(構造体がたまたま同じバイトサイズであっても)。

于 2012-06-07T03:28:45.003 に答える