示例#1
0
        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>();
            LatLngBounds latLngBounds = new LatLngBounds.Builder()
                                        .Include(new LatLng(region.Bounds.NorthEast.Lat, region.Bounds.NorthEast.Long))                 // Northeast
                                        .Include(new LatLng(region.Bounds.SouthWest.Lat, region.Bounds.SouthWest.Long))                 // Southwest
                                        .Build();
            OfflineTilePyramidRegionDefinition 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
                {
                    JsonObject 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);
        }