Coordinate Conversion C++

1122
6
09-15-2020 08:04 AM
RobLefebvre
New Contributor II

I am  trying to convert the view coordinates (Upper left and lower right corners) to real world coords in lat/lon, and can't figure it out.  We are on version 100.4 of the Qt C++ SDK.  

Here is the setup that I am using:

m_mapView = new MapGraphicsView(this);

// Create a map using the openStreetMap Basemap
m_map = new Map(BasemapType::OpenStreetMap,37.22296, -80.4139, 12);
m_mapView->setMap(m_map);

Any help would be appreciated

0 Kudos
6 Replies
LukeSmallwood
Esri Contributor

Hi Rob,

To do this you would make use of the MapGraphicsViews screenToLocation function - passing it the coordinates from your view. The upper left corner would be 0,0 in view coordinates and you should be able to get the lower right by taking the width/height of your map view.

This sample shows a different use of that functionality (reacting to a user clicking on the screen) but tghe principle is the same.

0 Kudos
RobLefebvre
New Contributor II

We definitely saw that call, and tried it, see below:

// Create the Widget view
m_mapView = new MapGraphicsView(this);

// Create a map using the openStreetMap Basemap
m_map = new Map(BasemapType::OpenStreetMap,37.22296, -80.4139, 12);
m_mapView->setMap(m_map);
const Point center(-80.252381, 37.227047, SpatialReference::wgs84());
const Viewpoint viewpoint(center, 300000.0);
m_map->setInitialViewpoint(viewpoint);

auto mapRect = m_mapView->rect();
auto b = m_mapView->screenToLocation(0.0, 0.0);
QString str = QString::number(b.x()) + QString::number(b.y());

When we do this, the b.x() and b.y() are NAN.  Is there an initialization step that I'm missing somewhere?  We can display the map, move around, load raster layers, etc., but no Lat/Lon

0 Kudos
LukeSmallwood
Esri Contributor

Hi Rob - can you confirm the map is loaded before you make the screenToLocation call? You can use the Map's doneLoading or loadStatusChanged signals to wait for that.

0 Kudos
RobLefebvre
New Contributor II

We added a connect for the signal(s) mentioned, and they are NOT being hit.  We tried both to a Lambda and a method, but  none were executed.  We also set a QTimer to fire every second, and re-try to get the info from the map, but to no avail.  

void TestMap::mapDoneLoading(Esri::ArcGISRuntime::Error loadError)
{
    auto mapRect = m_mapView->rect();
    auto b = m_mapView->screenToLocation(0.0, 0.0);
    QString str = QString::number(b.x()) + QString::number(b.y());
     QString sceneRectUL = CoordinateFormatter::toLatitudeLongitude(Point(m_mapView->rect().topLeft().x(), m_mapView->rect().topLeft().y()), LatitudeLongitudeFormat::DecimalDegrees, 4);
     qDebug() << "Scene Coords:  "<< sceneRectUL;
}

connect(m_map, &Map::doneLoading, this, &TestMap::mapDoneLoading);


0 Kudos
LukeSmallwood
Esri Contributor

Hi Rob - does  the map render correctly? e.g. do you see the OpenStreetmap basemap you set above? If it's rendering then the map is loading.

One possibility could be that you connect to the signal after loading is complete. The loading cycle will be kicked off as soon as you call m_mapView->setMap(m_map);. Can you make sure you set up the connection as soon as you construct the map?

Another observation is that you are setting the initial viewpoint for the map after you set it on the mapView (m_map->setInitialViewpoint(viewpoint);). For this to have an affect you need to do that before passing the map to the mapView as well.

0 Kudos
RobLefebvre
New Contributor II
This morning, in the doneLoading slot connection, the transformations don't work.  
However, if we capture a mouse click, and try to do transformations there, they are working fine.
Also, if we set a timer for 1 second, after we set the map and view up, the translations work fine too.  
Strange, for sure...
0 Kudos