3

enter image description hereI want to draw corridor using MKPolygon over MKMap using Mapkit. I have one route from station A to B.

I have MKMapRects around route for drawing corridor. Now i want to merge all rectangles in single Polygon and that is my Corridor along with route. How to join all the rectangles in single Polygon .

No.of rectangles : 160

Here i am attaching sample image indicating what i needed.

Here is code snippet.

for(int i=0;i<[self.boundingRectsArr count];i++) {

            lat1 = [[[self.boundingRectsArr objectAtIndex:i] valueForKey:@"xLT"] doubleValue];
    long1 = [[[self.boundingRectsArr objectAtIndex:i] valueForKey:@"yLT"] doubleValue];

    lat2 = [[[self.boundingRectsArr objectAtIndex:i] valueForKey:@"xRT"] doubleValue];
    long2 = [[[self.boundingRectsArr objectAtIndex:i] valueForKey:@"yRT"] doubleValue];
            lat3 = [[[self.boundingRectsArr objectAtIndex:i] valueForKey:@"xRB"] doubleValue];
    long3 = [[[self.boundingRectsArr objectAtIndex:i] valueForKey:@"yRB"] doubleValue];
    lat4 = [[[self.boundingRectsArr objectAtIndex:i] valueForKey:@"xLB"] doubleValue];
    long4 = [[[self.boundingRectsArr objectAtIndex:i] valueForKey:@"yLB"] doubleValue];

            CLLocationCoordinate2D rect[5];
            rect[0] = CLLocationCoordinate2DMake(lat1, long1);
    rect[1] = CLLocationCoordinate2DMake(lat2, long2);
    rect[2] = CLLocationCoordinate2DMake(lat3, long3);
    rect[3] = CLLocationCoordinate2DMake(lat4, long4);
            rect[4] = CLLocationCoordinate2DMake(lat1, long1);

    MKPolygon* polyCorridor = [MKPolygon polygonWithCoordinates:rect count:5];
    polyCorridor.title = @"Colorado";
    [self.map addOverlay:polyCorridor]; 
}

Thanks in Advance. Welcome to your answers.

Regards, Sagar P.enter image description here

sagarcool89
  • 1,366
  • 8
  • 16
  • Are there any assumptions that we can make? Are all the rectangles the same size? Are they squares? Do you have the rectangles available, sorted, in order along the path? – Nate Apr 07 '13 at 08:41
  • It would also be easiest to give you a solution with actual **code** if you could show us what kind of data structure you have as an **input** to this algorithm? For example, is it a `NSArray` of `MKMapRect`, ordered along the route? Also, are you 100% sure that what you want is a `MKPolygon`? Or are you just trying to draw a route on a map? Would it be better if the sides were smoothed (which `MKPolygon` doesn't give you)? – Nate Apr 07 '13 at 09:06
  • @Nate : Yes I have all MKMapRect Array in sorted Order as route. Let me attach final OutPut what i want actully and How its looks like. – sagarcool89 Apr 08 '13 at 05:03
  • @Nate : I have attached final Image, Please have a look its look like tube around the route (corridor around route) with perticular distance. suppose its 10 KM. – sagarcool89 Apr 08 '13 at 05:06
  • So, it sounds like you want a final corridor where the width of the corridor is the same, for the whole route? Is that correct? I'm just curious. How is it that you **start** with an array of **rectangles**? Normally, I would assume that someone would start with an array of points, so I'm curious what exactly one rectangle represents. – Nate Apr 08 '13 at 05:11
  • Finally, I just want to make sure that you want a true corridor, where the width is an actual *distance* (for example, 1 km). That means that as the map zooms out, the corridor will *look* thinner, not stay the same visual width. – Nate Apr 08 '13 at 05:13
  • let me tell you what corridor represents you. i have two contacts like A and B. i have drawn route from set of points. now i have all contacts in my Database.I am specifying distance suppose 8 Km. I have to draw corridor along with route of 8 Km distance and display all reachable objects along with route. – sagarcool89 Apr 08 '13 at 05:17
  • What do you mean by *reachable*? Reachable by driving? Is the 8km in your example the *length* of the route (distance from Point A to Point B, along the route), or the *width* of the corridor (thickness of the tube)? – Nate Apr 08 '13 at 05:19
  • thickness of tube is 8 KM, forget reachable objects. – sagarcool89 Apr 08 '13 at 05:22
  • @Nate : still any confusion ? – sagarcool89 Apr 08 '13 at 05:58

2 Answers2

0

So, to be fast:

  • Draw corrigor: draw second thick line.
  • Highlight objects in corridor: calculate them without a polygon union, but with distance-to-route approach.

You could draw two lines in your overlay implementation, one, thin, for route, and one for a corridor - thiiick and semi-transparent, you could try to calculate point to km ratio using data MKMapView provides and calculate the thick line width. And for the objects on map you want to highlight - you could use different approaches, there are number of algos for finding points near curve or a straight line. You even could be rather straightforward: split the route into straight lines and check the distance of all the objects - that would be very slow, but it will work (sure you'll need to google for those complex algos for that).

MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
0

Simple, first convert the MKMapRects to MKPolygons, then create the union of all the MKPolygons using this library: https://github.com/SunGard-Labs/MKPolygon-GPC unfortunately its not free.

malhal
  • 26,330
  • 7
  • 115
  • 133