Xamarin.Forms.Maps on a Master Detail Page

LotokLotok GBMember ✭✭

I seem to be opening a can of worms here with a change I have made. The change was to plug an issue with Android 4.4 not recognising OnLayout in a custom MasterDetailRenderer.

The change was simply not having the Detail Page as a NavigationPage. I just made it the ContentPage directly. Interesting unexpected side effect of this with Xamarin.Forms.Maps which is on that ContentPage.

Before, with the NavigationPage the map when instantiated passed in a MapSpan and set the map location and zoom level. With the DetailPage just being a ContentPage now, that no longer works. The map still works ok but it initializes with a maximum zoom out no matter what. I have tried simply wrapping the ContentPage back into a NavigationPage and it all works again.

My Map does have a custom renderer also, so I have been playing with options on that level without much luck. Anyone else seen this and able to point me in the right direction?

Best Answer

  • LotokLotok GBMember ✭✭
    Accepted Answer

    I have put in a hacky solution for now, would quite like to understand the reason NavigationPage has an impact if anyone is able to help point to to some reasons or possible sources I could check out.

    For now I have added a boolean flag in the custom renderer for intialized and a property in the custom map for position. I then added this line to the OnMapReady

        if (!_initialized)
                {
                    var pos = ((MapWithTapGesture)Element).LastKnownPostition;
                    _map.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(pos.Latitude, pos.Longitude), 18));
                    _initialized = true;
            }
    

    Which will default the map to the position provided to the property on the custom map and default the zoom to 18. After that the zoom level is tracked and all movements will maintain the users chosen zoom. I am not sure if the IF statement is really required here. I wasn't certain if the OnMapReady would be called again for a redraw on movement so left that IF in place to be certain.

    As I said, a bit hacky to get around a Xamarin.Forms issue that I cannot find documented anywhere

Answers

  • LotokLotok GBMember ✭✭
    Accepted Answer

    I have put in a hacky solution for now, would quite like to understand the reason NavigationPage has an impact if anyone is able to help point to to some reasons or possible sources I could check out.

    For now I have added a boolean flag in the custom renderer for intialized and a property in the custom map for position. I then added this line to the OnMapReady

        if (!_initialized)
                {
                    var pos = ((MapWithTapGesture)Element).LastKnownPostition;
                    _map.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(pos.Latitude, pos.Longitude), 18));
                    _initialized = true;
            }
    

    Which will default the map to the position provided to the property on the custom map and default the zoom to 18. After that the zoom level is tracked and all movements will maintain the users chosen zoom. I am not sure if the IF statement is really required here. I wasn't certain if the OnMapReady would be called again for a redraw on movement so left that IF in place to be certain.

    As I said, a bit hacky to get around a Xamarin.Forms issue that I cannot find documented anywhere

Sign In or Register to comment.