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>
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;
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));
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));
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; }
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 second map doesnt work because it's defined as mercator spatial reference but you are treating it as if it were geographic.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.