I am just trying to get Lat/Long on a left mouse click.
private void MapView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point pt = e.GetPosition(MainMapView);
MapPoint mapLocation = _mapView.ScreenToLocation(pt);
if (mapLocation == null)
{
locStr = "BAD LOCATION!";
}
...
}
I get BAD LOCATION every time (the return MapPoint is null). I have debugged the e.GetPosition and it is valid value based on the location of the map view I click on. There doesn't seem to any documentation as to why ScreenToLocation would return null give two valid points.
I'm guessing my _mapView isn't initialized correctly, but I don't know what else I need to do. I can can scroll around and zoom in and out with the mouse, so the actual view seems to be working just fine.
Any ideas what is going on here?
What version of the API are you using, v100 or v10.2? What is your map's SpatialReference? The only time I'm able to return null from a ScreenToLocation call is when my MapView has no SpatialReference set yet.
Thank you. The question now is how do you set the SpatialReference? I
thought it was set but as a debug test i put the following lines in my code
right before the call to ScreenToLocation.
SpatialReference mapRef = _mapView.Map.SpatialReference;
SpatialReference viewSpRef = _mapView.SpatialReference;
The first one returns a valid Spatial Reference instance. The second one
returns null? The only thing I have read is that the Map in the MapView
needs to be set in order to get the spacial reference. However the Map is
obviously set. Is this an order of operations thing perhaps (i.e. I need
to set the map on the mapview later than I am doing).
Thank you for your help
MapView.SpatialReference should get it's value from it's Map.SpatialReference. SpatialReference can either be set from Map constructor or based on the layer's SpatialReference. Can you check if the SpatialReferencedChanged event fires for the MapView?
How are you creating the map? I tried these and MapView eventually gets SpatialReference that when MouseLeftButtonDown is raised, ScreenToLocation has a valid value.
public MainWindow()
{
InitializeComponent();
MyMapView.SpatialReferenceChanged += MyMapView_SpatialReferenceChanged;
// Map.SR will be set, View.SR will not be set.
MyMapView.Map = new Map(SpatialReferences.Wgs84);
// Map and View SR will later be set once layer is loaded.
// MyMapView.Map = new Map(Basemap.CreateTopographic());
}
private void MyMapView_SpatialReferenceChanged(object sender, EventArgs e)
{
// View.SR must have a value
}