Use custom map style in Xamarin Forms?

I have successfully implemented Google Maps in my Xamarin Forms project, but now I want to change its style. I want to use the style from SnazzyMaps , but I do not know how to do it. I read on the forums that you can download json from SnazzyMaps into the application, but I don't know how to do it.

+4
source share
2 answers

In your custom Xamarin.FormsGoogleMap renderer, you can set the style using json content:

Xamarin.Android example:

googleMap.SetMapStyle(MapStyleOptions.LoadRawResourceStyle(this, Resource.Raw.map_style_night));

Xamarin.iOS example:

googleMapView.MapType = MapViewType.Normal; // Must be normal
var styleResource = NSBundle.MainBundle.GetUrlForResource("map_style_night", "json");
googleMapView.MapStyle = MapStyle.FromUrl(styleResource, null); // DO NOT pass an NSError, hard-crash / SIGSEGV

: NSError MapStyle.FromUrl MapStyle.FromJson Xamarin.Google.iOS.Maps(v2.1.0.2), (SIGSEGV). , NSError out var, , json ( iOS Google v2.4.30121.0, Xamarin / 2.1.0.2 ).

enter image description here

+3

Xaml:

     <StackLayout VerticalOptions="FillAndExpand" Padding="1,10,0,0">
            <maps:Map        
                x:Name="MyMap" 
                IsShowingUser="true"/>
        </StackLayout>

:

var assembly = typeof(MapPage).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream("MyApp.Helpers.Style.json");
            string Json = "";
            using (var reader = new StreamReader(stream))
            {
                Json = reader.ReadToEnd();
            }
            MyMap.MapStyle = MapStyle.FromJson(Json);

.json Embedded Resource.

0

Source: https://habr.com/ru/post/1683476/


All Articles