GET XY of center of map

2211
12
09-23-2019 12:30 PM
jaykapalczynski
Frequent Contributor

Can I call and set the XY location of center of the map with a button to variables?  Any example?

Maybe each time the map moves it resets the variables?

0 Kudos
12 Replies
jaykapalczynski
Frequent Contributor

If I set my spatial reference to:

Map{
   id: map
   spatialReference: SpatialReference { wkid:2150 }

All I want to do is click a button and have it display the center of map coordinates in UTM Zone 17N Nad83

Cant figure out how to do that

0 Kudos
KeithLarson1
MVP Alum

There is a MapView function that allows you to convert screen coordinates to Map coordinates called screenToLocation https://developers.arcgis.com/qt/latest/qml/api-reference/qml-esri-arcgisruntime-mapview.html#screen.... You can get the screen coordinate of the center of your MapView object that contains your map by usig the QML function mapToGlobal and pass it the local center (width / 2, height / 2) of the MapView. You can do something like this in a button onClicked event handler:

...
let center = myMapView.mapToGlobal(Qt.point(myMapView.width / 2, myMapView.height / 2));
let mapCenter = myMapView.screenToLocation(center.x, center.y);‍‍‍‍
...

I hope this helps!

Keith

0 Kudos
jaykapalczynski
Frequent Contributor

I put the below in OnClick of a button:

let center = MapView.mapToGlobal(Qt.point(MapView.width / 2, MapView.height / 2));
let mapCenter = MapView.screenToLocation(center.x, center.y);
console.log("mapCenter: " + mapCenter);

I get this error:

.qml:3420: TypeError: Property 'mapToGlobal' of object [object Object] is not a function

Do I need to import something at the top of the QML 

0 Kudos
KeithLarson1
MVP Alum

Oh, I assumed that mapToGlobal would work for MapView because Qt Creator's intellisense said it was available even though it throws the error. The easiest way to get around it probably is to wrap the MapView in an Item object and set the size of the Item to the size that you want the MapView to be. Then, just fill the MapView in the Item and use the Item's mapToGlobal funciton and center to get the screen coordinates.

0 Kudos
jaykapalczynski
Frequent Contributor

Just started this QML stuff....all of that right over my head....any help or examples you can give would be most appreciated....if not I will keep looking thanks for your thoughts...

I have to leave things dynamic so the map expands to various phones, tables etc...

We are talking about getting the XY location on the map itself right....  -76.564654, 37.545345

0 Kudos
KeithLarson1
MVP Alum

I just realized there is an even easier way than I initially suggested. There is a visibleArea property of MapView that returns a Polygon of the area of the map that is in view. You can handle the signal when the visibleArea changes as show below and get the Point object at the center of the visibleArea and display it.

MapView {
  id: myMapView
  anchors.fill: parent

  onVisibleAreaChanged: {
    let center = visibleArea.extent.center;
    // Display center point
    ...
  }

  Map {
    ...
  }
}
‍‍‍‍‍‍‍‍‍‍‍‍‍
jaykapalczynski
Frequent Contributor

All I get on this is :  console.log(center);

qml: QmlPoint(0x208cec665c0)

And If I try and return JSON I get an error and it blows up

I need to get the X and Y Value somehow

onVisibleAreaChanged: {
    let center = visibleArea.extent.center;
    // Display center point
    console.log(JSON.stringify(center));
}
0 Kudos
jaykapalczynski
Frequent Contributor

Think I might have gotten it with :

var test5 = visibleArea.extent.center.x;
var test6 = visibleArea.extent.center.y;

Testing

0 Kudos
jaykapalczynski
Frequent Contributor

OK I am returning accurate XY locations with the above.....Although I am returning them in Web Aux Sphere....

I tried to change the Spatial reference of the map but I am grabbing the center from the MapView and I cannot set the spatial reference in the MapView....ahahahah uuuuuggggggggg

I guess I just need to convert from Web Aux Sphere to UTM then on the fly somehow....I thought the setting of the spatial reference in the map would do that but I guess not

Any thoughts?

MapView{
    id: mapView

    onVisibleAreaChanged: {
        var center = visibleArea.extent.center;

        var test4 = visibleArea.extent.center.x;
        console.log(test4);
        var test5 = visibleArea.extent.center.y;
        console.log(test5);

    }


    Map{
        id: map
        spatialReference: SpatialReference { wkid:26917 }

// SNIP
0 Kudos