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>
The second map doesnt work because it's defined as mercator spatial reference but you are treating it as if it were geographic.
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?
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);
}
graphic.Geometry = new MapPoint(lng, lat, new SpatialReference(4326));
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));
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;
}
MyMap.Extent = newExtent;
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;