2

I am trying to hide the whole map except a zone, for that I am using a polygon with a hole in the area that I want to show.

The problem is that depending on the zoom the empty area is covered with the color of the polygon or the polygon loses its color.

This is the code:

//polygon hide all world map
float delta = 0.1f; //0.1f

List points = Arrays.asList(new LatLng(90, -180),

    new LatLng(-90+delta, -180+delta),

    new LatLng(-90+delta, 0),

    new LatLng(-90+delta, 180-delta),

    new LatLng(0, 180-delta),

    new LatLng(90-delta, 180-delta),

    new LatLng(90-delta, 0),

    new LatLng(90-delta, -180+delta),

    new LatLng(0,-180+delta));

PolygonOptions rectOptions = new PolygonOptions();
rectOptions.addAll(points);

//hole
ArrayList<LatLng> a = new ArrayList<>();
a.add(new LatLng(centro.getLatitud1(), centro.getLongitud1()));
a.add(new LatLng(centro.getLatitud2(), centro.getLongitud1()));
a.add(new LatLng(centro.getLatitud2(), centro.getLongitud2()));
a.add(new LatLng(centro.getLatitud1(), centro.getLongitud2()));
Iterable<LatLng> iterator = a;
rectOptions.addHole(iterator);
// Get back the mutable Polygon
rectOptions.fillColor(ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null));

polygon = mMap.addPolygon(rectOptions);   

Images:

All correct

All correct

polygon colour dissapear

polygon colour dissapear

Hole is filled with the color of the polygon

Hole is filled with the color of the polygon

If I keep zooming everything turns right.

Im using 'com.google.android.gms:play-services:9.6.1'

I do not know how to fix it and I am a noob in maps, if someone knows how to do it or knows some other method to do what I want I would be very grateful.

doblejota93
  • 171
  • 1
  • 1
  • 8

1 Answers1

1

Your code is correct. There is a known open bug (Issue 5241).

As a workaround, I would recommend you to take a look at my answer here (You can draw rectangular holes instead of circular ones).

Community
  • 1
  • 1
antonio
  • 18,044
  • 4
  • 45
  • 61
  • I found your answer a few days ago and implement it, the problem is that when I move on the map, the empty area follows me instead of remaining static, do you know if the area with color can keep static or simulate it somehow? – doblejota93 Dec 30 '16 at 17:58
  • You can try using the new `OnCameraMoveListener` and `OnCameraIdleListener` instead of the deprecated `OnCameraChangeListener`. I will update my answer as soon as I can – antonio Dec 30 '16 at 20:45