Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use custom map style in Xamarin Forms?

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

like image 234
Radu Avatar asked Mar 09 '23 09:03

Radu


1 Answers

In your custom Xamarin.Forms GoogleMap renderer, you can set the style with the 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

Note: Do not pass an NSError instance to MapStyle.FromUrl or MapStyle.FromJson with the current Xamarin.Google.iOS.Maps binding (v2.1.0.2) as this will cause a hard-crash (SIGSEGV). I had to create a custom binding to allow NSError as an out var in order to determine if the json is parsed correctly (also needed to the latest fixes in Google iOS Map v2.4.30121.0 as Xamarin is binding/bundling the older 2.1.0.2 version).

enter image description here

like image 108
SushiHangover Avatar answered Mar 16 '23 09:03

SushiHangover