Select to view content in your preferred language

Invalid spatial reference

1305
6
01-05-2012 11:16 AM
TylerRothermund
Emerging Contributor
Im getting this error when I switched to a different map.

This map works fine
http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer

This map give me the error
http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer



 private void AddMarker(UnitUI unit)
        {
            var graphic = new Graphic() { Symbol = GetSymbol(unit) };

            double lat = unit.Base.Latitude;
            double lng = unit.Base.Longitude;

            var point = new MapPoint(lng, lat);
            point.SpatialReference = MyMap.SpatialReference;

            graphic.Geometry = new WebMercator().FromGeographic(point);  <--error here "Invalid spatial reference "

            GraphicsLayer.Graphics.Add(graphic);
        }



        <esri:Map WrapAround="True" x:Name="MyMap" Grid.Row="1"  Grid.ColumnSpan="2">
            <esri:ArcGISTiledMapServiceLayer Initialized="MyMapLayer_Initialized" 
                                             Url="{StaticResource MapUrl}"/>
            <esri:GraphicsLayer Initialized="MyMapLayer_Initialized">
            </esri:GraphicsLayer>
            <i:Interaction.Behaviors>
                <esri:MaintainExtentBehavior />
            </i:Interaction.Behaviors>
        </esri:Map>
0 Kudos
6 Replies
by Anonymous User
Not applicable
The second map doesnt work because it's defined as mercator spatial reference but you are treating it as if it were geographic.
0 Kudos
TylerRothermund
Emerging Contributor
The second map doesnt work because it's defined as mercator spatial reference but you are treating it as if it were geographic.


Thanks for the reply.
Any idea what I need to change to fix it?  Do I need to use something other than WebMercator for the 2nd map?   And how would I know what mercator to use if I allow the user to switch between different maps?

The whole Mercator thing is foriegn to me,  I don't remember ever having to deal with it using Bing/Google maps.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Any idea what I need to change to fix it? Do I need to use something other than WebMercator for the 2nd map? And how would I know what mercator to use if I allow the user to switch between different maps?


If your map is already using Geographical coordinates, you don't need any projection (assuming that your lng and lat are always geographical coordinates whatever the map SR)

So, this should work:
var point = new MapPoint(lng, lat, new SpatialReference(4326)); 
if (MyMap.SpatialReference.WKID == 4326) // Geographic coordinates
{
  graphic.Geometry = point;
}
else
{
  graphic.Geometry = new WebMercator().FromGeographic(point); 
}


That being said, from 2.3, the graphics layer is able to autoproject the geometry.
So this sould work as well:
graphic.Geometry = new MapPoint(lng, lat, new SpatialReference(4326)); 
0 Kudos
TylerRothermund
Emerging Contributor
If your map is already using Geographical coordinates, you don't need any projection (assuming that your lng and lat are always geographical coordinates whatever the map SR)

So, this should work:
var point = new MapPoint(lng, lat, new SpatialReference(4326)); 
if (MyMap.SpatialReference.WKID == 4326) // Geographic coordinates
{
  graphic.Geometry = point;
}
else
{
  graphic.Geometry = new WebMercator().FromGeographic(point); 
}


That being said, from 2.3, the graphics layer is able to autoproject the geometry.
So this sould work as well:
graphic.Geometry = new MapPoint(lng, lat, new SpatialReference(4326)); 



So that seemed to fix the original problem but now my ZoomTo isn't working

 private void ZoomTo(double lat, double lng, int level)
        {
            var newResolution = GetResolution(level);
            double halfWidth = newResolution * MyMap.ActualWidth / 2;
            double halfHeight = newResolution * MyMap.ActualHeight / 2;
            var point = new MapPoint(lat, lng, new SpatialReference(4326));

            Envelope newExtent = new Envelope(point.X - halfWidth, point.Y - halfHeight, point.X + halfWidth, point.Y + halfHeight);
            MyMap.Extent = newExtent;
        }
0 Kudos
JenniferNery
Esri Regular Contributor
The problem seems to be that your map is WKID=3857 and you want to zoom to an extent with WKID=4326. Since you already have the resolution, maybe you should call ZoomToResolution, there is an overload for including MapPoint http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Map~ZoomTo...
0 Kudos
DominiqueBroux
Esri Frequent Contributor
           MyMap.Extent = newExtent;

To get that working the newExtent must be in the same SpatialReference than the map.
So if your map is using WebMercator, your extent must be in WebMercator as well.

Try something like:

var point = new MapPoint(lat, lng, new SpatialReference(4326));
if (!point.SpatialReference.Equals(MyMap.Spatialreference))
   point = new WebMercator().FromGeographic(point); // assuming your map is either in web mercator or in geographical coordinates. Might be more complex if you need to take all others cases into account

Envelope newExtent = new Envelope(point.X - halfWidth, point.Y - halfHeight, point.X + halfWidth, point.Y + halfHeight);
MyMap.Extent = newExtent;
0 Kudos