0

My app currently enables the user to tap anywhere and get a marker drawn there. I want a closed polygon automatically drawn when there is more than 2 markers. How do I achieve that? The first and last points should be connected to each other to create the closed shape.

My code so far that draws a polyline, but does not close it as a polygon:

// polyline setup -> array made of clicked coordinates
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];
GMSMutablePath *rect = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
    event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
    event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
    [rect addCoordinate:event];
}

// draw polyline
GMSPolyline *polygon = [GMSPolyline polylineWithPath:rect];
konyv12
  • 716
  • 2
  • 8
  • 23

4 Answers4

0

Have you tried adding the first coordinates as the last point?

// polyline setup -> array made of clicked coordinates
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];
GMSMutablePath *rect = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
    event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
    event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
    [rect addCoordinate:event];
}

// add the first point as the last point, to "close" the polygon
event.latitude = [[latitudeTappedCoordinates objectAtIndex:0] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:0] floatValue];
[rect addCoordinate:event];

// draw polyline
GMSPolyline *polygon = [GMSPolyline polylineWithPath:rect];
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • That's cool, but when it gets to more than 3 coordinates: http://imgur.com/a/N1XGy. How could I keep it up always "closed"? – konyv12 Mar 15 '17 at 19:48
  • Sorry, figured you were already sorting the coordinates into a non-crisscrossing pattern. That's an entirely different question. – DonMag Mar 15 '17 at 19:50
  • Let's talk about it. – konyv12 Mar 15 '17 at 19:52
  • The answer by "andand" on this question looks like a good, clear solution... http://stackoverflow.com/questions/7369710/sorting-polygons-points – DonMag Mar 15 '17 at 20:06
0
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];

You can then set the polygon's fill color to be clear.

Pang
  • 9,564
  • 146
  • 81
  • 122
Hyperfine
  • 91
  • 7
0

Try this;

    - (void)mapView:(GMSMapView *)mapView
    didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {

        if (latitudeTappedCoordinates == nil){
        longitudeTappedCoordinates = [[NSMutableArray alloc] init];
        latitudeTappedCoordinates = [[NSMutableArray alloc] init];
        }
        [latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
        [longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];

    }

Call below method to show polygon on map:

    -(void)drawPolygon {

     if ([longitudeTappedCoordinates count]>2) {
         GMSMutablePath *rect = [GMSMutablePath path];
         CLLocationCoordinate2D event;

         for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
             event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
             event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
             [rect addCoordinate:event];
         }
         event.latitude = [[latitudeTappedCoordinates objectAtIndex:0] floatValue];
         event.longitude = [[longitudeTappedCoordinates objectAtIndex:0] floatValue];
         [rect addCoordinate:event];

         GMSPolygon *polygon = [[GMSPolygon alloc] init];
         polygon.path = rect;
         polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.2f];
         polygon.strokeColor = [UIColor blackColor];
         polygon.strokeWidth = 2;
         polygon.map = _mapView;
    }
    else {
         NSLog(@"Tap again on Map");
    }
}
nikdange_me
  • 2,949
  • 2
  • 16
  • 24
0

In the GMS polygon first and last points always connected. You just need to pass all the coordinates in proper oder.

- (void)mapView:(GMSMapView *)mapView
    didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {

    if (latitudeTappedCoordinates == nil){
        longitudeTappedCoordinates = [[NSMutableArray alloc] init];
        latitudeTappedCoordinates = [[NSMutableArray alloc] init];
     }
     [latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
     [longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];

    if([longitudeTappedCoordinates count]>2)
     {
        [self drawPolygonOnMap]
      }

}

Then change polygon.fillColor to check polygon closed or not

-(void)drawPolygon {
         GMSMutablePath *rect = [GMSMutablePath path];
         CLLocationCoordinate2D event;

         for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
             event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
             event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
             [rect addCoordinate:event];
         }
         event.latitude = [[latitudeTappedCoordinates objectAtIndex:0] floatValue];
         event.longitude = [[longitudeTappedCoordinates objectAtIndex:0] floatValue];
         [rect addCoordinate:event];

         GMSPolygon *polygon = [[GMSPolygon alloc] init];
         polygon.path = rect;
         polygon.fillColor = [UIColor grayColor];
         polygon.strokeColor = [UIColor blackColor];
         polygon.strokeWidth = 2;
         polygon.map = _mapView;
}
Sid Mhatre
  • 3,272
  • 1
  • 19
  • 38