How to set SpatialReference in C++

778
2
Jump to solution
05-30-2021 11:46 PM
Kanzakiryu
New Contributor

Hello.

I'm trying to use ArcGIS with C++.

I don't know how to set  SpatialReference for maps in C++.

Is there any way to do this other than setting it up in the new Map()?

 

header:

 

#ifndef CPPMAPS_H
#define CPPMAPS_H

namespace Esri
{
namespace ArcGISRuntime
{
class Map;
class MapQuickView;
}
}

#include <QObject>
#include <QMouseEvent>

using namespace Esri::ArcGISRuntime;

class CppMaps : public QObject
{
    Q_OBJECT

    Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)

public:
    explicit CppMaps(QObject* parent = nullptr);
    ~CppMaps() override;

signals:
    void mapViewChanged();

public slots:
    void fnSlotTest(QMouseEvent &mouseEvent);

private:
    MapQuickView* mapView() const;
    void setMapView(MapQuickView* mapView);

    Map* m_map = nullptr;
    MapQuickView* m_mapView = nullptr;

    //SpatialReference sp(6668);  <-GetError
};

 

 

 

 

cpp:

 

 

#include "CppMaps.h"

#include "Basemap.h"
#include "Map.h"
#include "MapQuickView.h"
#include <QUrl>

CppMaps::CppMaps(QObject* parent /* = nullptr */):   QObject(parent)
{
        m_map = new Map(BasemapStyle::ArcGISTopographic, this);
}

CppMaps::~CppMaps()
{
}

MapQuickView* CppMaps::mapView() const
{
    qDebug() << "TEST";
    return m_mapView;
}

// Set the view (created in QML)
void CppMaps::setMapView(MapQuickView* mapView)
{
    if (!mapView || mapView == m_mapView)
    {
        return;
    }

    m_mapView = mapView;
    m_mapView->setMap(m_map);

    qDebug() << m_map->spatialReference().wkid();

    connect(m_mapView, &MapQuickView::mouseClicked, this, &CppMaps::fnSlotTest);

    emit mapViewChanged();
}

void CppMaps::fnSlotTest(QMouseEvent &mouseEvent)
{
    qDebug() << mouseEvent.x();
    qDebug() << mouseEvent.y();

    const Point cPoint = m_mapView->screenToLocation(mouseEvent.x(), mouseEvent.y());

    qDebug() << cPoint.x();
}

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
LucasDanzinger
Esri Frequent Contributor

The spatial reference can be set in one of two ways. The first, most common way, is to add a layer to your map. The first layer, typically the basemap, will dictate what the spatial reference of the map will be. In the case of the ArcGIS Basemaps, most are in Web Mercator, so a map created with these basemaps will be in web mercator. The second option is to explicitly instantiate a map with a SpatialReference object, and then add your layers to your map after. The caveat here is that some layers, such as tiled layer/vector tiled layer basemaps, do not support reprojecting on the fly, so you will need to use layers that can be projected on the fly or publish your own basemap data in the spatial reference you choose.

 

More info on spatial references can be found here - https://developers.arcgis.com/qt/spatial-and-data-analysis/spatial-references/ 

View solution in original post

0 Kudos
2 Replies
LucasDanzinger
Esri Frequent Contributor

The spatial reference can be set in one of two ways. The first, most common way, is to add a layer to your map. The first layer, typically the basemap, will dictate what the spatial reference of the map will be. In the case of the ArcGIS Basemaps, most are in Web Mercator, so a map created with these basemaps will be in web mercator. The second option is to explicitly instantiate a map with a SpatialReference object, and then add your layers to your map after. The caveat here is that some layers, such as tiled layer/vector tiled layer basemaps, do not support reprojecting on the fly, so you will need to use layers that can be projected on the fly or publish your own basemap data in the spatial reference you choose.

 

More info on spatial references can be found here - https://developers.arcgis.com/qt/spatial-and-data-analysis/spatial-references/ 

0 Kudos
Kanzakiryu
New Contributor

OK, It worked.

Thanks!!!

0 Kudos