public Task <OfflineRegionDto> CreateOfflineRegion(LatLngBoundsDto bounds, int zoomLevel = 8) { var tcs = new TaskCompletionSource <OfflineRegionDto>(); LatLngBounds latLngBounds = new LatLngBounds.Builder() .Include(new LatLng(bounds.Ne.Latitude, bounds.Ne.Longitude)) .Include(new LatLng(bounds.Sw.Latitude, bounds.Sw.Longitude)) .Build(); var offlineDefinition = new OfflineTilePyramidRegionDefinition(Style.MapboxStreets, latLngBounds, 1, zoomLevel, Application.Context.Resources.DisplayMetrics.Density); var jsonObject = new JSONObject(); jsonObject.Put("REGION_NAME", "Chennai"); string json = jsonObject.ToString(); byte[] metadata = Encoding.UTF8.GetBytes(json); _offlineManager.CreateOfflineRegion(offlineDefinition, metadata, new CreateOfflineRegionCallback(Application.Context) { OnListCallback = region => { region.SetDownloadState(OfflineRegion.StateActive); tcs.SetResult(Transform(region)); }, OnErrorCallback = error => { tcs.SetResult(null); } }); return(tcs.Task); }
public Task <OfflinePack> DownloadMap(OfflinePackRegion region, Dictionary <string, string> packInfo) { var tcs = new TaskCompletionSource <OfflinePack>(); var latLngBounds = new LatLngBounds.Builder() .Include(region.Bounds.NorthEast.ToLatLng()) // Northeast .Include(region.Bounds.SouthWest.ToLatLng()) // Southwest .Build(); var definition = new OfflineTilePyramidRegionDefinition( region.StyleURL, latLngBounds, region.MinimumZoomLevel, region.MaximumZoomLevel, Android.App.Application.Context.Resources.DisplayMetrics.Density); byte[] metadata = null; if (packInfo != null) { //var binFormatter = new BinaryFormatter(); //var mStream = new MemoryStream(); //binFormatter.Serialize(mStream, packInfo); //metadata = mStream.ToArray(); try { var jsonObject = new JsonObject(); foreach (KeyValuePair <string, string> pair in packInfo) { jsonObject.AddProperty(pair.Key, pair.Value); } var json = new Java.Lang.String(jsonObject.ToString()); metadata = json.GetBytes(JSON_CHARSET); System.Diagnostics.Debug.WriteLine("Encoding metadata succeeded: " + metadata.Length.ToString()); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Failed to encode metadata: " + ex.Message); } } offlineManager.CreateOfflineRegion( definition, metadata, new CreateOfflineRegionCallback( (reg) => { reg.SetDownloadState(OfflineRegion.StateActive); tcs.TrySetResult(reg.ToFormsPack()); }, (msg) => { System.Diagnostics.Debug.WriteLine("[ERROR] Couldn't create offline pack: " + msg); tcs.TrySetResult(null); } ) ); return(tcs.Task); }
public static OfflinePackRegion ToFormsRegion(this OfflineTilePyramidRegionDefinition definition) { return(new OfflinePackRegion { Bounds = new Bounds(new Position(definition.Bounds.SouthWest.Latitude, definition.Bounds.SouthWest.Longitude), new Position(definition.Bounds.NorthEast.Latitude, definition.Bounds.NorthEast.Longitude)), StyleURL = definition.StyleURL, MinimumZoomLevel = definition.MinZoom, MaximumZoomLevel = definition.MaxZoom }); }