2012-06-07 71 views
4

我在理解如何向MKMapView添加多個覆蓋圖時遇到了一些麻煩。向MKMapView添加多個覆蓋圖

我有一個數組路由跟蹤。跟蹤的路線是CLLocations的數組。所以我有一個數組的數組。

我能夠採取一條路線,並在地圖視圖中繪製它。但是,我需要將所有路線繪製到地圖視圖上。

因此,這裏是我如何去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; 
+0

什麼是'self.routeLine'和'self.routeLineView'用於上述代碼中的_besides_?顯示調用addOverlay或addOverlays的代碼。 – Anna

+0

@AnnaKarenina增加了。 – random

+0

所以routeLine和routeLineView不會在問題中顯示的代碼中使用,對嗎? – Anna

回答

4

routeLinerouteLineView變量一次只能指向一個覆蓋和覆蓋視圖,因此您理論上需要一組這樣的變量。

但是,根據顯示的代碼,您並不需要首先引用這些參考。

我建議刪除routeLinerouteLineView變量。

然後,您只需在本地創建覆蓋對象,並在viewForOverlay返回所請求的overlay參數的視圖。

loadRoute方法中,您可以遍歷routesArray併爲每個路由創建一個本地覆蓋對象,並且在其上調用addOverlay

還有一種更簡單的方法可以構建一個限制所有路線的地圖矩形,因此您可以設置地圖的區域以將其全部顯示出來。

例子:

-(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)的大小。
它們不是一回事(即使結構碰巧是相同的字節大小)。

+1

你真棒!非常感謝! – random