android how to draw a circle on google map - android

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

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.

How to draw transparent circles on Google Maps API

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

Android: How can I use the Google maps

Can someone explain how I can use the Google maps API in my project?
Cause I want to draw a marker on the map while moving the map with my finger.
But I don't know how to achieve this.
I am using the following code:
camCenter = mMap.getCameraPosition().target;
if (camCenter != null && camCenter != camCenterFirst) {
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(camCenter.latitude,camCenter.longitude));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
camCenterFirst = camCenter;
}
First you have to add a marker to your Map like this:
LatLng pos = new LatLng(53.557115, 10.023159);
Marker m = mMap.addMarker(new MarkerOptions().position(pos).title(""));
The example above, gives you a default position of the marker.
Then you can change the position of the marker with following command:
m.setPosition(new LatLng(latitude,longitude));
Hope this helps!
public void showOnMap(Double lat,Double lng,String place)
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat,
lng)).title(""+place);
mMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(lat, lng)).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
double radiusInMeters = 5000.0;
int strokeColor = 0xffff0000; //red outline
int shadeColor = 0x44ff0000; //opaque red fill
CircleOptions circleOptions = new CircleOptions().center(new LatLng(lat,lng)).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8);
mMap.addCircle(circleOptions);
}
I have done a complete demo of what you are looking for,
Check this Github code
https://github.com/cseshaiban/GeoApp

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