Re-using Map on different pages in Xamarin Forms UWP

686
2
08-24-2021 07:57 AM
KrishnaShinde
New Contributor II

We have this application for UWP developed in Xamarin Forms where we display the ESRI map in different pages. The map is created from an offline vtpk file that we have on the device. Every time we land on a page with MapView control, we do the following

 

var mapPackageFolder = Constants.TpkFolderLocation;
var mapPackageFile = @"WebM_BaseMap_no_contour.vtpk";

var mapPackagePath = System.IO.Path.Combine(mapPackageFolder,mapPackageFile);

var tileCache = new VectorTileCache(mapPackagePath);

ArcGISVectorTiledLayer vectorTiledLayer = new ArcGISVectorTiledLayer(tileCache);

var map = new Map(new Basemap(vectorTiledLayer));

 

 

Instead of creating the map from the scratch each time, can we re-use it in different pages in the app?
Is there any standard way of doing this?

0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor

The first rule you need to be aware of is: A map can only be attached to one MapView at any point in time, but you can move a Map from one MapView to another.

So that gives you two choices:
1. Every time you navigate away from a page with a MapView, set its Map property to null to "unhook" it. You could rely on garbage collection and things going out of scope, but it can be pretty non-deterministic, so safer to do it explicitly. You can then assign the map again to a new MapView on a new page.

2. Create one static instance of a MapView. Every time you navigate away from a page, remove the MapView from that page, then insert it on the page you need it on. A custom control that does this for you could make it easier (ie it'll add and remove a MapView inside it based on load/unload of the parent control).

Option 1 is simpler and saves you loading the map every time, but requires you to restore other state like Graphics, current viewpoint etc.

Option 2 is a bit more advanced, but also saves your from creating and destroying mapviews, restoring mapview state, and also avoids recreating all the MapView rendering resources which can be a bit  expensive. Because of this, I've seen option 2 often recommended with many different map UI controls, and the ArcGIS Runtime isn't that different.

KrishnaShinde
New Contributor II

Thanks for your response.
Will this give a go.

0 Kudos