Amruta Deshmukh
Amruta Deshmukh

Reputation: 1045

Android: How do I add a translucent geofence over the map

Android: How do I add a translucent geofence over a map. I.e., i can fill it with a color but the map below is still visible.

This is what I wish to achieve

Upvotes: 1

Views: 1484

Answers (4)

Pablo Baxter
Pablo Baxter

Reputation: 2234

Use Color.argb(). To learn more, go to the Color class reference page (http://developer.android.com/reference/android/graphics/Color.html). Here is an example of making the blue translucent fill:

Color.argb(
    100, //This is your alpha.  Adjust this to make it more or less translucent
    Color.red(Color.BLUE), //Red component.
    Color.green(Color.BLUE),  //Green component.
    Color.blue(Color.BLUE));  //Blue component.

This will make the blue translucent color you wanted. If you have a custom hex color (0xFFFFFFFF), just adjust the first byte (0xFF......) to make it more or less translucent.

EDIT

Here is a simple function that you can use to pass in the alpha and color to adjust it:

int alphaAdjust(int alpha, int color) {
    return Color.argb(
            alpha, //This is your alpha.  Adjust this to make it more or less translucent
            Color.red(color), //Red component.
            Color.green(color),  //Green component.
            Color.blue(color));  //Blue component.
}

Upvotes: 2

chandra
chandra

Reputation: 1

Something like this:

LatLng latLng = ...
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(latLng)
             .fillColor(Color.argb(64, 0, 255, 0))
             .strokeColor(Color.GREEN)
             .strokeWidth(1)
             .radius(100);

Notice the first argument in Color.argb() method (value 64), that argument controls the alpha/transculency of the circle, in this case it will be a transculent green circle. change the first argument value from 0 to 255 to achieve the transculency you desire. Here is the Color.argb method javadoc - http://developer.android.com/reference/android/graphics/Color.html#argb(int, int, int, int)

Upvotes: 0

N Dorigatti
N Dorigatti

Reputation: 3540

You can create a Circle, it is very easy: Having your geofence details already in place (geofLocation and geofRadius) you can do:

// Instantiates a new CircleOptions object and defines the center and radius
CircleOptions circleOptions = new CircleOptions()
.strokeColor(Color.BLACK) //Outer black border
.fillColor(Color.TRANSPARENT) //inside of the geofence will be transparent, change to whatever color you prefer like 0x88ff0000 for mid-transparent red
    .center(geofLocation) // the LatLng Object of your geofence location
    .radius(geofRadius)); // The radius (in meters) of your geofence

// Get back the mutable Circle
Circle circle = myMap.addCircle(circleOptions);

Upvotes: 1

Florian Barth
Florian Barth

Reputation: 1392

Try the examples listed in the documentation. I'd suggest to use a translucent circle to indicate your geofence.

Upvotes: 0

Related Questions