Add a button on top of MapView

I'm wanting to add a go-to-my-current-location button on top of an ArcGIS .NET MapView in my CustomMap custom renderer within a Xamarin Forms PCL grid layout. It seems like there's a few ways to do it--use ArcGIS to overlay a button/graphic, make my custom renderer a UIView and add a map layer and a button layer, and create the custom renderer at the page level and mimic my PCL layout. I'd like to go for one of the first two options, and I'm currently trying to get the second one (make my custom renderer a UIView and add a map layer and a button layer) to work.

Anyone have any ideas? I have very little experience with UIView, so I'd appreciate any insight.

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace PortableApp.iOS
{
    public class CustomMapRenderer : ViewRenderer<CustomMap, UIView>
    {
        MapView mapView;
        UIView layout;    

        protected override void OnElementChanged(ElementChangedEventArgs<CustomMap> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                layout = new UIView(new CGRect(0, 0, 500, 400));

                // create button
                UIButton button = new UIButton();
                button.TitleLabel.Text = "Current Location";

                // create mapview and map
                mapView = new MapView();
                ...

                // add views to layout
                layout.AddSubviews(mapView, button);

                SetNativeControl(layout);
            }                   
        }    
    }
}

Best Answer

  • TimKleierTimKleier US
    Accepted Answer

    Solved. Just needed to create the button in the correct way, and then specify the Frame property on the button and MapView:

    layout = new UIView(new CGRect(0, 0, 500, 400));
    
    // create button
    UIButton button = UIButton.FromType(UIButtonType.RoundedRect);
    button.SetTitle("Current Location", UIControlState.Normal);
    button.Frame = new CGRect(160, 100, 140, 40);
    
    // create mapview and map
    mapView = new MapView();
    mapView.Frame = new CoreGraphics.CGRect(0, 0, 400, 400);
    ....
    
    // add views to layout
    layout.AddSubviews(mapView, button);
    

Answers

  • TimKleierTimKleier USMember
    Accepted Answer

    Solved. Just needed to create the button in the correct way, and then specify the Frame property on the button and MapView:

    layout = new UIView(new CGRect(0, 0, 500, 400));
    
    // create button
    UIButton button = UIButton.FromType(UIButtonType.RoundedRect);
    button.SetTitle("Current Location", UIControlState.Normal);
    button.Frame = new CGRect(160, 100, 140, 40);
    
    // create mapview and map
    mapView = new MapView();
    mapView.Frame = new CoreGraphics.CGRect(0, 0, 400, 400);
    ....
    
    // add views to layout
    layout.AddSubviews(mapView, button);
    
Sign In or Register to comment.