5
(1)

Draw a circle around the Google Map Marker using Android in Xamarin C#

Are you looking for Xamarin c# code for your Android application to draw a circle around the Google Map Marker? If yes, then this post is for you.

This post doesn’t include Google Map implementation logic in Xamarin.

Prerequisite: Google Map implementation using Xamarin C#.

Here are the steps you can follow to draw a circle around the Google Map Marker.

Step 1: Add following namespace in your MainActivity (or activity where you have Google Map logic)

using Android.Gms.Maps.Model; 
using Android.Graphics;

Step 2: Update your OnMapReady method with the following code.

//Your MainActivity class may like this
public class MainActivity: Activity, IOnMapReadyCallback 
 { 
 private GoogleMap googleMap; 
 protected override void OnCreate(Bundle bundle) 
 { 
 base.OnCreate(bundle); 
 SetContentView(Resource.Layout.Main); 
 ....
 } 
public void OnMapReady(GoogleMap map)
        {
            this.googleMap = map;

            this.googleMap.UiSettings.ZoomControlsEnabled = true;
            this.googleMap.UiSettings.CompassEnabled = true;
            this.googleMap.UiSettings.MyLocationButtonEnabled = false;
            
             double lat = 18.516726;
             double lng = 73.856255;
             LatLng myLatLng = new LatLng(lat, lng);
             this.googleMap.Clear();
             
             var myMarker = new MarkerOptions();
             myMarker.SetPosition(myLatLng)
                .SetTitle("Pune")
                .SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.map_icon));
             DrawCircle(googleMap, myLatLng);
             this.googleMap.AddMarker(myMarker);
            
        }
...
}

Step 3: Implement the DrawCircle method.

public void DrawCircle(GoogleMap gMap, LatLng latlong)
        {
            Circle circle;
            var CircleMaker = new CircleOptions();
            CircleMaker.InvokeCenter(latlong); // 
            CircleMaker.InvokeRadius(100); //Radius in Circle 
            CircleMaker.InvokeStrokeWidth(4);
            CircleMaker.InvokeStrokeColor(Android.Graphics.Color.ParseColor("#e61DC2D1")); //Circle Color 
            CircleMaker.InvokeFillColor(Color.Argb(034, 29, 194, 209));
            CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlong, 15); //Map Zoom Level 
            circle = gMap.AddCircle(CircleMaker); //gmap Add Circle 
        }

Step 4: Run the application. 

Hope this code helps you to create a circle around the Google Map Marker.

Read more Xamarin related posts: https://jigneshdarji.com/category/xamarin/

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Share this post on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?