Spatial Reference Woes

1239
2
08-25-2016 02:47 PM
JoeMadrigal
New Contributor III

I'm adding a TPK layer with spatial reference "WGS84 Web Mercator Auxilary Sphere" to a map and the SpatialReference of the map and mapview still has null for the spatial reference.  According to the online help, I thought that if you add a layer to your map, it takes the spatial reference of that layer:

To set the SpatialReference of the MapView, a Map must be added to the MapView.Map Property and either: (1) the InitialViewpoint must be set (including the Envelope's Extent and SpatialReference) or (2) at least one layer (which has SpatialReference information) must be added to the Map's LayerCollection (typically via the Map.Layers.Add() Method).

So first, after looking at the tpk in ArcMap, it does say it has spatial reference Web Mercator mentioned above. In the XAML I created a MapVIew (called mapViewMain) and then a map inside the MapView (called mapMain).  In the code I do:

Snippet

ArcGISLocalTiledLayer tLayer = new ArcGISLocalTiledLayer(filename);tLayer.ID = strName;tLayer.DisplayName = strName;mapViewMain.Map.Layers.Add(tLayer);

After doing this my mapViewMain and mapMain both have Spatial Reference of null.  Am I doing something wrong or forgetting something?

Thanks,

Joe

0 Kudos
2 Replies
AnttiKajanus1
Occasional Contributor III

That should do the trick. If you hook into MapView.SpatialReferenceChanged do you see it being triggered? There might be different events run before that is set (ie. layerloaded) so just make sure that there is no errors on loading the layer. 

0 Kudos
JoeMadrigal
New Contributor III

Thanks for the tip antti kajanus‌, this led me to try to access the spatial reference at different events.  If I try to do anything in the MapView.LayerLoaded, then the spatial reference is not known yet (even though I made sure it was the last layer that was loaded).  Then I tried to access the spatial reference in the MapView.NavigationCompleted event which worked, and I just removed the event call after the first time (I see this done a lot in ESRI samples).  But I may want to use that in the future for another purpose.  So Like you mentioned, I tried the MapView.SpatialReferenceChanged event.  This worked also and in another suggestion, I added the 'await mapViewMain.LayersLoadedAsysnc(), before any other calls.  This is what I will stick too before adding graphics or whatever I need after the layers are completely loaded and spatial reference also loaded.

Joe

0 Kudos