How to draw transparent circles on Google Maps API - android

I try to draw circles on the Google Maps API, which are transparent.
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(lat, lon))
.radius(50.0)
.strokeColor(Color.TRANSPARENT)
.strokeWidth(1)
.fillColor(Color.CYAN); //
But I like to draw them transparently, so that I can see the map behind? How could I do that?

For mGoogleMap as GoogleMap object, with latitude longitude given as
LatLng currentLatLon = new LatLng(lat double value, lng double value);
This is the way to draw circle in google maps
mGoogleMap.addCircle(new CircleOptions()
.center(currentLatLon)
.radius(150)
.strokeColor(Color.RED)
.fillColor(0x220000FF)
.strokeWidth(5)
);

This will add the transparent circle around the marker
Circle mCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))
.radius(500)
.strokeWidth(0)
.strokeColor(Color.parseColor("#2271cce7"))
.fillColor(Color.parseColor("#2271cce7")));
For the Transparent Color you can set it to any Color using i.e #2271cce7
In the above the actual color code is #71cce7 and the first two digits of 22 indicate the transparency which can be set to any value to either increase or decrease transparency.
View Sample

Related

How to retrieve latitude and longitude of Google Maps android

How can I retrieve latitude and longitude of Google Maps data from my database and display it to the user on the map in android ?
I have a database mysql containing some fields as longitude and latitude.
What is the best way to do such?
Is it possible through a Volley library?
If there is an example of this, it is good to me
Yes you can draw stuff in the onMapReady callback. You can create Markers, draw circles, draw polygons.
To add a marker just call:
LatLng point = new LatLng(exampleLat, exampleLng);
mGoogleMap.addMarker(new MarkerOptions().position(point));
Here is an example of how to draw a circle:
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(lat, lng))
.radius(radius)
.fillColor(Color.RED)
.strokeColor(Color.TRANSPARENT);
Circle circle = mGoogleMap.addCircle(circleOptions);
Here is a Polygon:
PolygonOptions polygonalOptions = new PolygonOptions()
.fillColor(Color.RED)
.strokeColor(Color.TRANSPARENT)
.clickable(true);
for (List<Double> latLng : myPolygon.path) {
LatLng point = new LatLng(latLng.get(0), latLng.get(1));
polygonalOptions.add(point);
}
mGoogleMap.addPolygon(polygonalOptions);
PS: Writing from my phone sorry for any mistakes or formatting issues.

How to create a circle around the user's location (blue dot) on Google Maps? I am creating a Geofence app

I am creating a Geofence app where I want it to only create a geofence or circle on the user's current location. So far the solution is to create a marker on the map, but I don't intend on using map markers. Wherever the blue dot is located on the map, I want it to generate a geofence circle around it. What can I do?
private void startGeofence() {
Log.i(TAG, "startGeofence");
Geofence geofence = createGeofence( geoFenceMarker.getPosition(), GEOFENCE_RADIUS);
GeofencingRequest geofencingRequest = createGeofenceRequest(geofence);
addGeofence(geofencingRequest);
}
private Marker geoFenceMarker;
private Circle geofenceLimit;
private void drawGeofence() {
if (geofenceLimit != null){
geofenceLimit.remove();
}
CircleOptions circleOptions = new CircleOptions()
.center( geoFenceMarker.getPosition())
.strokeColor(Color.argb(50, 70,70,70))
.fillColor( Color.argb(100, 150,150,150) )
.radius( GEOFENCE_RADIUS );
geofenceLimit = googleMap.addCircle( circleOptions );
}
You can use Circle class to draw the circle in users current location.
Circle drawCircle = map.addCircle(new CircleOptions()
.center(new LatLng(loc.latitude, loc.longitude))
.radius(1000)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
You can get more info from: https://developers.google.com/android/reference/com/google/android/gms/maps/model/Circle
Hope this helps to you.

Google Marker is moving while using achor in MarkerOption

I was trying to place a marker in Google Map using the following code.
googleMap.clear();
//Dont know why this is 4.8f...
googleMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.google_marker))
.position(ll).flat(true)
.anchor(0.5f, 0.5f));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(ll)
.zoom(initMapZoom) // Sets the zoom
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
cameraPosition = new CameraPosition.Builder()
.target(ll)
.zoom(finalMapZoom) // Sets the zoom
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Now, If I zoom in / out the marker moves from the actual place. If I just remove this
.anchor(0.5f, 0.5f));
Its working fine. The problem is I would like to put the marker in the upper/top position of the screen. So I used .anchor(0.5f, 4.8f)); which is also wrong according to the documentation . The value should be less or equal to 1.
Any ideas to resolve this issue??
Thanks in Advance.

android how to draw a circle on google map

I am using the new API(Google Map API V2) for my android application, i have done creating the map and adding markers to it, now my task is to manually create a circle around any of the marker and also i want to provide a functionality to the user that he can increase the radius of that circle accordingly, for this i have given a bar, when user increases that bar the radius of circle will increase and vice versa.
If anybody knows how to do this using Google Map API V2 then please help,
try this code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tag_map);
// Drawing circle on the map
drawCircle(new LatLng(Latitude, Longitude));
}
private void drawCircle(LatLng point){
// Instantiating CircleOptions to draw a circle around the marker
CircleOptions circleOptions = new CircleOptions();
// Specifying the center of the circle
circleOptions.center(point);
// Radius of the circle
circleOptions.radius(20);
// Border color of the circle
circleOptions.strokeColor(Color.BLACK);
// Fill color of the circle
circleOptions.fillColor(0x30ff0000);
// Border width of the circle
circleOptions.strokeWidth(2);
// Adding the circle to the GoogleMap
googleMap.addCircle(circleOptions);
}
Add the following code in onMapReady() method:
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(latitude, longitude))
.radius(10000)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
where map is the object of GoogleMap class.
Google Maps can use semi-transparent color for the circle.
private var circle: Circle? = null
private fun drawCircle(latitude: Double, longitude: Double, radius: Double) {
val circleOptions = CircleOptions()
.center(LatLng(latitude, longitude))
.radius(radius)
.strokeWidth(1.0f)
.strokeColor(ContextCompat.getColor(context!!, R.color.color1))
.fillColor(ContextCompat.getColor(context!!, R.color.color2))
circle?.remove() // Remove old circle.
circle = googleMap?.addCircle(circleOptions) // Draw new circle.
}
Kotlin version of #Lalit Kushwah
map.addCircle(
CircleOptions()
.center([Your LatLng])
.radius(10000.0)
.strokeColor(Color.RED)
.fillColor(Color.BLUE)
)
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(new LatLng(location.getLatitude(),location.getLongitude()));
circleOptions.radius(700);
circleOptions.fillColor(Color.TRANSPARENT);
circleOptions.strokeWidth(6);
mMap.addCircle(circleOptions);
geocoder = new Geocoder(MapsActivity.this, getDefault());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(location.getLatitude(), location.getLongitude()));
mMap.addMarker(markerOptions.title(String.valueOf(location)));
try this

How to show a radius in maps android?

I have a eight different Longitude and latitude values in that any one value have longitude and latitude and remaining wont have values in that case I have to show a radius for that one value. Can anyone help me to solve this problem.
Thanks
GoogleMap has method addCircle so your just need to call it with appropriate params:
GoogleMap map;
MarkerOptions markerOptions = new MarkerOptions();
LatLng position = new LatLng(50, 50);
markerOptions.position(position);
Marker marker = map.addMarker(markerOptions);
Circle circle = map.addCircle(
new CircleOptions()
.center(position)
.radius(100)
.strokeWidth(0f)
.fillColor(getActivity().getResources().getColor(R.color.someColor))
);

Categories

Resources