TorbenK / TK.CustomMap

Extended Map Control
MIT License
142 stars 109 forks source link

How to use map with route getting exception - layout not occurred yet? #345

Open LeoJHarris opened 6 years ago

LeoJHarris commented 6 years ago

I know somebody had this issue the issue was closed but I cant figure out how to setup this plugin I am getting:

`Unhandled Exception:

Java.Lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions. occurred`

Basically I have my map in the xaml:

<tk:TKCustomMap HorizontalOptions="FillAndExpand" MapReady="DirectionsMap_MapReady" x:Name="DirectionsMap" VerticalOptions="FillAndExpand" />

In my code behind I have:

`private async void DirectionsMap_MapReady(object sender, EventArgs e) { Plugin.Geolocator.Abstractions.Position myPosition = await Plugin.Geolocator.CrossGeolocator.Current.GetLastKnownLocationAsync();

        IEnumerable<Plugin.Geolocator.Abstractions.Position> addressPositions = await Plugin.Geolocator.CrossGeolocator.Current.GetPositionsForAddressAsync("157 Hobson St, Auckland");

        if (addressPositions.Any() && myPosition != null)
        {
            Plugin.Geolocator.Abstractions.Position pos = addressPositions.First();

            TKRoute route = new TKRoute
            {
                TravelMode = TKRouteTravelMode.Driving
            };

            Position myposition = new Position(myPosition.Latitude, myPosition.Longitude);
            Position toposition = new Position(pos.Latitude, pos.Longitude);
            route.Source = myposition;
            route.Destination = toposition;
            route.Color = Color.Red;
            route.TravelMode = TKRouteTravelMode.Any;
            route.LineWidth = 2;
            routes.Add(route);

            DirectionsMap.RouteCalculationFinishedCommand = new Command(() =>
            {
                if (routes.All(i => i.IsCalculated))
                {
                    DirectionsMap.FitToMapRegions(routes.Select(i => i.Bounds), true);
                }
            });
        }
        else
        {
            Acr.UserDialogs.UserDialogs.Instance.Toast("Hotel not found");
        }
    }`

Whats wrong with this code the error I am getting is in the android library within the FitToMapRegions I think when I looked at the source code of the plugin.

Is there something I am doing wrong here??

vincentcastagna commented 5 years ago

Hello,

I think you sould try to set width and height request to your map. Here's an example :

      _customMap = new CustomMap(Constants.InitialLatitudeMap, Constants.InitialLongitudeMap, 200)
            {
                HeightRequest = 80,
                WidthRequest = 960,
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                IsShowingUser = true,
                IsClusteringEnabled = false
            };