create flag on image like google map in android

I'm going to put flag on my image like google map in android app. when user Click on the flags display comment that points. and When the image is zoomed the location of flags does not change But did not know how to do it, Please guide me in this regard.
thanks

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    Sounds like what you want is an ItemizedOverlay for your Maps control.

    using System.Collections.Generic;
    using System.Linq;
    
    using Android.Content;
    using Android.GoogleMaps;
    using Android.Graphics.Drawables;
    using Android.Graphics;
    
    namespace MonoDroid.SimpleOverlayItem
    {
        class MyItemizedOverlay : ItemizedOverlay
        {
            private List<OverlayItem> overlayItems = new List<OverlayItem>();
            private Context context;
    
            public MyItemizedOverlay(Context context, Drawable drawable)
                : base(BoundCenterBottom(drawable)) 
            {
                this.context = context;
                Populate();
            }
    
            public override int Size() 
            {
                //Always call Populate() when modifying the overlayItems, otherwise this will throw exceptions.
                return overlayItems.Count;
            }
    
            public void AddOverlayItem(OverlayItem item)
            {
                overlayItems.Add(item);
                Populate();
            }
    
            public void RemoveOverlayItem(OverlayItem item)
            {
                overlayItems.Remove(item);
                Populate();
            }
    
            public void ClearOverlayItems()
            {
                overlayItems.Clear();
                Populate();
            }
    
            public List<OverlayItem> OverlayItems
            {
                //Remember to call Populate() if modifying this list.
                get { return overlayItems; }
            }
    
            protected override Java.Lang.Object CreateItem(int index)
            {
                return overlayItems.ElementAt(index);
            }
        }
    }
    

    This can then be used in your MapActivity to add elements to the map like so:

    var marker = Resources.GetDrawable(Resource.Drawable.Icon);
    var myItemizedOverlay = new MyItemizedOverlay(this, marker);
    
    var mapView = FindViewById<MapView>(Resource.Id.MyMapView);
    mapView.Overlays.Add(myItemizedOverlay);
    
    var point1 = new GeoPoint((int)(55.785213 * 1E6), (int)(12.522182 * 1E6));
    var overlayItem1 = new OverlayItem(point1, "DTU", "Anker Engelunds Vej 101");
    overlayItem1.SetMarker(Resources.GetDrawable(Resource.Drawable.AnotherIcon));
    myItemizedOverlay.AddOverlayItem(overlayItem1);
    
    var point2 = new GeoPoint((int)(55.816034 * 1E6), (int)(12.532547 * 1E6));
    var overlayItem2 = new OverlayItem(point2, "Brüel & Kjær", "Skodsborgvej 307C");
    overlayItem1.SetMarker(Resources.GetDrawable(Resource.Drawable.AnotherIcon2));
    myItemizedOverlay.AddOverlayItem(overlayItem2);
    
    mapView.PostInvalidate();
    

    Given that your icons for the overlay items are saved as Drawables in your app.

    To add an action when one of the items are tapped override OnTap in the ItemizedOverlay

Sign In or Register to comment.