protected override void OnCreate(Android.OS.Bundle savedInstanceState) { base.OnCreate(savedInstanceState); AddBaseLayer(CartoBaseMapStyle.CartoBasemapStyleDefault); TileDataSource source = CreateTileDataSource(); // Get decoder from current layer, // so we wouldn't need a style asset to create a decoder from scratch MBVectorTileDecoder decoder = (MBVectorTileDecoder)(MapView.Layers[0] as VectorTileLayer).TileDecoder; // Remove default baselayer MapView.Layers.Clear(); // Add our new layer var layer = new VectorTileLayer(source, decoder); MapView.Layers.Insert(0, layer); // Zoom to the correct location MapPos rome = BaseProjection.FromWgs84(new MapPos(12.4807, 41.8962)); MapView.SetFocusPos(rome, 0); MapView.SetZoom(13, 0); }
protected override void OnCreate(Android.OS.Bundle savedInstanceState) { base.OnCreate(savedInstanceState); AddOnlineBaseLayer(CartoBaseMapStyle.CartoBasemapStyleDefault); // Get decoder from current layer, // so we wouldn't need a style asset to create a decoder from scratch MBVectorTileDecoder decoder = (MBVectorTileDecoder)(MapView.Layers[0] as VectorTileLayer).TileDecoder; // Remove default baselayer MapView.Layers.Clear(); // Do the actual copying and source creation on another thread so it wouldn't block the main thread System.Threading.Tasks.Task.Run(delegate { TileDataSource source = CreateTileDataSource(); var layer = new VectorTileLayer(source, decoder); RunOnUiThread(delegate { // However, actual layer insertion should be done on the main thread MapView.Layers.Insert(0, layer); }); }); // Zoom to the correct location MapPos rome = BaseProjection.FromWgs84(new MapPos(12.4807, 41.8962)); MapView.SetFocusPos(rome, 0); MapView.SetZoom(13, 0); }
public override void ViewDidLoad() { base.ViewDidLoad(); // Add default base layer AddOnlineBaseLayer(CartoBaseMapStyle.CartoBasemapStyleVoyager); Projection projection = MapView.Options.BaseProjection; TileDataSource source = FileUtils.CreateTileDataSource("rome_carto-streets", "mbtiles"); // Get decoder from current layer, // so we wouldn't need a style asset to create a decoder from scratch MBVectorTileDecoder decoder = (MBVectorTileDecoder)(MapView.Layers[0] as VectorTileLayer).TileDecoder; // Remove default baselayer MapView.Layers.Clear(); // Add our new layer var layer = new VectorTileLayer(source, decoder); MapView.Layers.Insert(0, layer); // Zoom to the correct location MapPos rome = projection.FromWgs84(new MapPos(12.4807, 41.8962)); MapView.SetFocusPos(rome, 0); MapView.SetZoom(13, 0); }
public MapContainer() { Map = new MapView(); var timer = new Stopwatch(); timer.Start(); // NOTE: Uncomment the timer creation and BeginInvokeOnMainThread to apply a workaround //var startTimer = new System.Threading.Timer((_) => //{ // Device.BeginInvokeOnMainThread(() => // { MBTilesTileDataSource testDataSource = new MBTilesTileDataSource(((App)App.Current).CityDataFile); CompiledStyleSet styleSet = new CompiledStyleSet(new ZippedAssetPackage(AssetUtils.LoadAsset("nutibright-v3.zip"))); MBVectorTileDecoder decoder = new MBVectorTileDecoder(styleSet); VectorTileLayer testLayer = new VectorTileLayer(testDataSource, decoder); Map.Layers.Add(testLayer); // }); //}, null, 10, System.Threading.Timeout.Infinite); timer.Stop(); Debug.WriteLine($"Done map initialization in {timer.ElapsedMilliseconds}ms"); MapPos pos = Map.Options.BaseProjection.FromWgs84(new MapPos(App.CITY_POS_X, App.CITY_POS_Y)); Map.SetFocusPos(pos, 0); Map.SetZoom(15, 0); Content = Map.ToView(); }
protected bool LoadNutiteqMapCommon() { // Set base projection EPSG3857 proj = new EPSG3857(); _mapViewer.Options.BaseProjection = proj; // note: EPSG3857 is the default, so this is actually not required // Set initial location and other parameters, don't animate _mapViewer.FocusPos = proj.FromWgs84(new MapPos(-0.8164, 51.2383)); // Berlin _mapViewer.Zoom = 2; _mapViewer.MapRotation = 0; _mapViewer.Tilt = 90; // Start package manager var packageManager = new NutiteqPackageManager("nutiteq.mbstreets", _downloadPackagePath); packageManager.Start(); // Import initial package if (packageManager.GetLocalPackage("world0_4") == null) { packageManager.StartPackageImport("world0_4", 1, _importPackagePath); } // Set bounding box String bbox = "bbox(-0.8164,51.2382,0.6406,51.7401)"; // London (about 30MB) if (packageManager.GetLocalPackage(bbox) == null) { packageManager.StartPackageDownload(bbox); } // Now can add vector map as layer // define styling for vector map UnsignedCharVector styleBytes = AssetUtils.LoadBytes("osmbright.zip"); MBVectorTileDecoder vectorTileDecoder = null; if (styleBytes != null) { // Create style set MBVectorTileStyleSet vectorTileStyleSet = new MBVectorTileStyleSet(styleBytes); vectorTileDecoder = new MBVectorTileDecoder(vectorTileStyleSet); } else { Log.Error("Failed to load style data"); } // Create online base layer (no package download needed then). Use vector style from assets (osmbright.zip) // comment in to use online map. Packagemanager stuff is not needed then // VectorTileLayer baseLayer = new NutiteqOnlineVectorTileLayer("osmbright.zip"); var baseLayer = new VectorTileLayer(new PackageManagerTileDataSource(packageManager), vectorTileDecoder); _mapViewer.Layers.Add(baseLayer); return(true); }
void UpdateLanguage(string code) { if (currentLayer == null) { return; } MBVectorTileDecoder decoder = (currentLayer as VectorTileLayer).TileDecoder as MBVectorTileDecoder; decoder.SetStyleParameter("lang", code); }
protected bool LoadNutiteqMapCommon() { // Set base projection EPSG3857 proj = new EPSG3857(); _mapViewer.Options.BaseProjection = proj; // note: EPSG3857 is the default, so this is actually not required // Set initial location and other parameters, don't animate _mapViewer.FocusPos = proj.FromWgs84(new MapPos(-0.8164, 51.2383)); // Berlin _mapViewer.Zoom = 2; _mapViewer.MapRotation = 0; _mapViewer.Tilt = 90; // Start package manager var packageManager = new NutiteqPackageManager("nutiteq.mbstreets", _downloadPackagePath); packageManager.Start(); // Import initial package if (packageManager.GetLocalPackage("world0_4") == null) { packageManager.StartPackageImport("world0_4", 1, _importPackagePath); } // Set bounding box String bbox = "bbox(-0.8164,51.2382,0.6406,51.7401)"; // London (about 30MB) if (packageManager.GetLocalPackage(bbox) == null) { packageManager.StartPackageDownload(bbox); } // Now can add vector map as layer // define styling for vector map UnsignedCharVector styleBytes = AssetUtils.LoadBytes("osmbright.zip"); MBVectorTileDecoder vectorTileDecoder = null; if (styleBytes != null) { // Create style set MBVectorTileStyleSet vectorTileStyleSet = new MBVectorTileStyleSet(styleBytes); vectorTileDecoder = new MBVectorTileDecoder(vectorTileStyleSet); } else { Log.Error("Failed to load style data"); } // Create online base layer (no package download needed then). Use vector style from assets (osmbright.zip) // comment in to use online map. Packagemanager stuff is not needed then // VectorTileLayer baseLayer = new NutiteqOnlineVectorTileLayer("osmbright.zip"); var baseLayer = new VectorTileLayer(new PackageManagerTileDataSource(packageManager), vectorTileDecoder); _mapViewer.Layers.Add(baseLayer); return true; }
public static void InitializePackageManager(string packageFolder, string importPackagePath, IMapView mapView, string downloadedPackage) { // Offline base layer // Define listener, definition is in same class above var packageManager = new CartoPackageManager("nutiteq.osm", packageFolder); packageManager.PackageManagerListener = new PackageListener(packageManager, downloadedPackage); // Download new package list only if it is older than 24h // Note: this is only needed if pre-made packages are used if (packageManager.ServerPackageListAge > 24 * 60 * 60) { packageManager.StartPackageListDownload(); } // Start manager - mandatory packageManager.Start(); // Import initial package if (packageManager.GetLocalPackage("world0_4") == null) { packageManager.StartPackageImport("world0_4", 1, importPackagePath); } // Now can add vector map as layer // Define styling for vector map BinaryData styleBytes = AssetUtils.LoadAsset("nutibright-v2a.zip"); MBVectorTileDecoder vectorTileDecoder = null; if (styleBytes != null) { // Create style set var vectorTileStyleSet = new CompiledStyleSet(new ZippedAssetPackage(styleBytes)); vectorTileDecoder = new MBVectorTileDecoder(vectorTileStyleSet); } else { Log.Error("Failed to load style data"); } // Create online base layer (no package download needed then). Use vector style from assets (osmbright.zip) // comment in to use online map. Packagemanager stuff is not needed then //VectorTileLayer baseLayer = new NutiteqOnlineVectorTileLayer("osmbright.zip"); var baseLayer = new VectorTileLayer(new PackageManagerTileDataSource(packageManager), vectorTileDecoder); mapView.Layers.Add(baseLayer); }
protected virtual void UpdateBaseLayer() { string styleAssetName = vectorStyleName + ".zip"; bool styleBuildings3D = false; if (vectorStyleName.Equals("nutibright3d")) { styleAssetName = BaseStyleFile; styleBuildings3D = true; } BinaryData styleBytes = AssetUtils.LoadAsset(styleAssetName); if (styleBytes == null) { Toast.MakeText(this, "Map style file must be in project assets: " + vectorStyleName, ToastLength.Short).Show(); return; } // Create style set CompiledStyleSet vectorTileStyleSet = new CompiledStyleSet(new ZippedAssetPackage(styleBytes)); vectorTileDecoder = new MBVectorTileDecoder(vectorTileStyleSet); // Set language, language-specific texts from vector tiles will be used vectorTileDecoder.SetStyleParameter("lang", vectorStyleLang); // OSM Bright style set supports choosing between 2d/3d buildings. Set corresponding parameter. if (styleAssetName.Equals(BaseStyleFile)) { vectorTileDecoder.SetStyleParameter("buildings3d", styleBuildings3D ? "1" : "0"); vectorTileDecoder.SetStyleParameter("markers3d", styleBuildings3D ? "1" : "0"); vectorTileDecoder.SetStyleParameter("texts3d", styleBuildings3D ? "1" : "0"); } // Create tile data source for vector tiles if (vectorTileDataSource == null) { vectorTileDataSource = CreateTileDataSource(); } // Remove old base layer, create new base layer if (BaseLayer != null) { MapView.Layers.Remove(BaseLayer); } BaseLayer = new VectorTileLayer(vectorTileDataSource, vectorTileDecoder); MapView.Layers.Insert(0, BaseLayer); }
public override void ViewDidLoad() { base.ViewDidLoad(); BinaryData bytes = AssetUtils.LoadAsset("carto-fonts.zip"); ZippedAssetPackage package = new ZippedAssetPackage(bytes); string css = JsonUtils.OfflinePackageCartoCSS; var decoder = new MBVectorTileDecoder(new CartoCSSStyleSet(css, package)); TileDataSource source = FileUtils.CreateTileDataSource("offline-packages", "mbtiles"); var layer = new VectorTileLayer(source, decoder); MapView.Layers.Add(layer); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); BinaryData bytes = AssetUtils.LoadAsset("carto-fonts.zip"); ZippedAssetPackage package = new ZippedAssetPackage(bytes); string css = JsonUtils.OfflinePackageCartoCSS; var decoder = new MBVectorTileDecoder(new CartoCSSStyleSet(css, package)); TileDataSource source = FileUtils.CreateTileDataSource(this, "offline-packages.mbtiles"); var layer = new VectorTileLayer(source, decoder); MapView.Layers.Add(layer); }
void UpdateBaseLayer(Section section, string selection) { if (section.Type != SectionType.Language) { currentOSM = section.OSM.Value; currentSelection = selection; } if (section.Type == SectionType.Vector) { if (currentOSM == "nutiteq.osm") { // Nutiteq styles are bundled with the SDK, we can initialize them via constuctor if (currentSelection == "default") { currentLayer = new CartoOnlineVectorTileLayer(CartoBaseMapStyle.CartoBasemapStyleDefault); } else if (currentSelection == "gray") { currentLayer = new CartoOnlineVectorTileLayer(CartoBaseMapStyle.CartoBasemapStyleGray); } else { currentLayer = new CartoOnlineVectorTileLayer(CartoBaseMapStyle.CartoBasemapStyleDark); } } else if (currentOSM == "mapzen.osm") { // Mapzen styles are all bundled in one .zip file. // Selection contains both the style name and file name (cf. Sections.cs in Shared) string fileName = currentSelection.Split(':')[0]; string styleName = currentSelection.Split(':')[1]; // Create a style set from the file and style BinaryData styleAsset = AssetUtils.LoadAsset(fileName + ".zip"); var package = new ZippedAssetPackage(styleAsset); CompiledStyleSet styleSet = new CompiledStyleSet(package, styleName); // Create datasource and style decoder var source = new CartoOnlineTileDataSource(currentOSM); var decoder = new MBVectorTileDecoder(styleSet); currentLayer = new VectorTileLayer(source, decoder); } ContentView.Menu.LanguageChoiceEnabled = true; ResetLanguage(); } else if (section.Type == SectionType.Raster) { // We know that the value of raster will be Positron or Darkmatter, // as Nutiteq and Mapzen use vector tiles // Additionally, raster tiles do not support language choice string url = (currentSelection == "positron") ? Urls.Positron : Urls.DarkMatter; TileDataSource source = new HTTPTileDataSource(1, 19, url); currentLayer = new RasterTileLayer(source); // Language choice not enabled in raster tiles ContentView.Menu.LanguageChoiceEnabled = false; } else if (section.Type == SectionType.Language) { if (currentLayer is RasterTileLayer) { // Raster tile language chance is not supported return; } UpdateLanguage(selection); } MapView.Layers.Clear(); MapView.Layers.Add(currentLayer); ContentView.Menu.Hide(); currentListener = null; currentListener = MapView.InitializeVectorTileListener(VectorLayer); }
void UpdateBaseLayer(Section section, string selection) { if (section.Type != SectionType.Language) { currentOSM = section.OSM.Value; currentSelection = selection; } if (section.Type == SectionType.Vector) { if (currentOSM == "nutiteq.osm") { // Nutiteq styles are bundled with the SDK, we can initialize them via constuctor if (currentSelection == "default") { currentLayer = new CartoOnlineVectorTileLayer(CartoBaseMapStyle.CartoBasemapStyleDefault); } else if (currentSelection == "gray") { currentLayer = new CartoOnlineVectorTileLayer(CartoBaseMapStyle.CartoBasemapStyleGray); } else { currentLayer = new CartoOnlineVectorTileLayer(CartoBaseMapStyle.CartoBasemapStyleDark); } } else if (currentOSM == "mapzen.osm") { // Mapzen styles are all bundled in one .zip file. // Selection contains both the style name and file name (cf. Sections.cs in Shared) string fileName = currentSelection.Split(':')[0]; string styleName = currentSelection.Split(':')[1]; // Create a style set from the file and style BinaryData styleAsset = AssetUtils.LoadAsset("styles/" + fileName + ".zip"); var package = new ZippedAssetPackage(styleAsset); CompiledStyleSet styleSet = new CompiledStyleSet(package, styleName); // Create datasource and style decoder var source = new CartoOnlineTileDataSource(currentOSM); var decoder = new MBVectorTileDecoder(styleSet); currentLayer = new VectorTileLayer(source, decoder); } Menu.LanguageChoiceEnabled = true; ResetLanguage(); } else if (section.Type == SectionType.Raster) { // We know that the value of raster will be Positron or Darkmatter, // as Nutiteq and Mapzen use vector tiles // Additionally, raster tiles do not support language choice string url = (currentSelection == "positron") ? Urls.Positron : Urls.DarkMatter; TileDataSource source = new HTTPTileDataSource(1, 19, url); currentLayer = new RasterTileLayer(source); // Language choice not enabled in raster tiles Menu.LanguageChoiceEnabled = false; } else if (section.Type == SectionType.Language) { if (currentLayer is RasterTileLayer) { // Raster tile language chance is not supported return; } UpdateLanguage(selection); } MapView.Layers.Clear(); MapView.Layers.Add(currentLayer); Menu.Hide(); currentListener = null; currentListener = MapView.InitializeVectorTileListener(VectorLayer); }
public static void Update(this MapView MapView, bool withListener, byte[] data, Action completed, Action <string> failed = null) { System.Threading.Tasks.Task.Run(delegate { var sourceId = Parser.GetSourceFromData(data); BinaryData styleAsset = new BinaryData(data); var package = new ZippedAssetPackage(styleAsset); var styleSet = new CompiledStyleSet(package); // UWP doesn't have a version released where simply changing the style set is supported, // need to clear layers and recreate the entire thing #if __UWP__ MapView.Layers.Clear(); var source = new CartoOnlineTileDataSource(OSM); var decoder = new MBVectorTileDecoder(styleSet); var layer = new VectorTileLayer(source, decoder); Device.BeginInvokeOnMainThread(delegate { MapView.Layers.Add(layer); if (completed != null) { completed(); } }); #else if (MapView.Layers.Count == 0) { if (sourceId == null) { sourceId = DefaultSourceId; } var source = new CartoOnlineTileDataSource(sourceId); MBVectorTileDecoder decoder = null; try { decoder = new MBVectorTileDecoder(styleSet); } catch (System.ApplicationException e) { if (failed != null) { failed(e.Message); return; } } var layer = new VectorTileLayer(source, decoder); if (withListener) { var popupSource = new LocalVectorDataSource(MapView.Options.BaseProjection); var popupLayer = new VectorLayer(popupSource); MapView.Layers.Add(popupLayer); layer.VectorTileEventListener = new VectorEventListener(popupSource); } Device.BeginInvokeOnMainThread(delegate { MapView.Layers.Add(layer); if (completed != null) { completed(); } }); } else { var decoder = (MBVectorTileDecoder)(MapView.Layers[MapView.Layers.Count - 1] as VectorTileLayer).TileDecoder; Device.BeginInvokeOnMainThread(delegate { decoder.CompiledStyle = styleSet; if (completed != null) { completed(); } }); } #endif }); }