I'm trying to get the MGRS string for a selected graphic in a graphic layer. I thought it would be fairly straight forward, but I'm getting an empty string from the toMgrs() call. I have two cases - Case A using 'var' for the Point, and Case B using 'property' for Point. When I try using project() on the point, I get a null. I appreciate any help. Here's the code:
onFindGraphicsComplete: {
unselectGraphic(selectedGraphicID)
if (graphicIDs.length > 0) {
selectedGraphicID = graphicIDs[0]
selectGraphic(selectedGraphicID)
if (isGraphicSelected(selectedGraphicID)) {
var selectedGraphic = graphic(selectedGraphicID);
// Case A: (using 'var')
var dummyPoint1 = selectedGraphic.geometry;
console.log("X_X: ", dummyPoint1.x, ", Y_Y: ", dummyPoint1.y);
// var dummyPoint2 = dummyPoint1.project(mainMap.spatialReference);
// console.log("X_X_X: ", dummyPoint2.x, ", Y_Y_Y: ", dummyPoint2.y);
console.log("MGRS: ", dummyPoint1.toMgrs(0, 10, false, false));
// Case B: (using 'property Point dummyPoint1_property', property Point dummyPoint2_property)
/*
dummyPoint1_property = selectedGraphic.geometry;
console.log("X_X: ", dummyPoint1_property.x, ", Y_Y: ", dummyPoint1_property.y);
dummyPoint2_property = dummyPoint1_property.project(mainMap.spatialReference);
console.log("X_X_X: ", dummyPoint2_property.x, ", Y_Y_Y: ", dummyPoint2_property.y);
console.log("MGRS: ", dummyPoint2_property.toMgrs(1, 10, false, false));
*/
} } }
Solved! Go to Solution.
Lucas,
I got it. I wasn't adding the spatial reference when I was adding graphics to the graphics layer. I assumed that when a graphic is added to a graphic layer that the graphic was set to the spatial reference of the graphic layer which it inherits from the bas map (tile package). I guess that was a bad assumption on my part.
-Rainer
Rainer-
Give this code a try-
import QtQuick 2.3
import ArcGIS.Extras 1.0
import ArcGIS.Runtime 10.26
Rectangle {
id: rootRectangle
property bool graphicSelected: false
property int graphicID
property double scaleFactor: System.displayScaleFactor
Map {
id: mainMap
anchors.fill: parent
wrapAroundEnabled: true
focus: true
grid {
visible: true
gridType: Enums.GridTypeMgrs
labelVisible: true
}
// Create the initial graphics through JSON
onStatusChanged: {
if (status === Enums.MapStatusReady) {
var graphic1 = {
geometry: {
spatialReference: {latestWkid: 3857,wkid:102100},
x: 16130193.189065285,
y: -3807126.3427607343
},
symbol: {
type: "esriSMS",
size: 20,
style: "esriSMSX",
color: "blue"
}
};
graphicsLayer.addGraphic(graphic1);
}
}
onMouseClicked: {
if (graphicSelected) {
graphicsLayer.graphic(graphicID).moveTo(mouse.mapPoint);
graphicsLayer.unselectGraphic(graphicID);
graphicSelected = false;
} else {
var tolerance = Qt.platform.os === "ios" || Qt.platform.os === "android" ? 4 : 1;
graphicsLayer.findGraphic(mouse.x, mouse.y, tolerance * scaleFactor);
}
}
ArcGISTiledMapServiceLayer {
url: "http://services.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer"
}
GraphicsLayer {
id: graphicsLayer
renderingMode: Enums.RenderingModeStatic
onFindGraphicsComplete: {
for (var i = 0; i < graphicIDs.length; i++) {
var graphicId = graphicIDs;
graphicID = graphicIDs;
if (!isGraphicSelected(graphicId)) {
selectGraphic(graphicId);
var selGraphic = graphicsLayer.graphic(graphicId);
console.log("Point Coordinates (102100):", JSON.stringify(selGraphic.geometry.json))
console.log("MGRS Coordinate", selGraphic.geometry.toMgrs(Enums.MgrsConversionModeAutomatic, 10, false, false))
graphicSelected = true;
} else {
unselectGraphic(graphicId);
graphicSelected = false;
}
}
}
}
}
}
Lucas,
I ran your code and got the following results:
qml: Point Coordinates (102100): {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":16130193.1890653,"y":-3807126.34276073}
qml: MGRS Coordinate 55HCE0234447420694343
When I ran my code I got the following:
qml: Point Coordinates (102100): {"x":13388865.2146819,"y":1586203.41858292}
qml: MGRS Coordinate:
How is it I'm missing 'spatialReference' and how do I fix it? When I tried using project() using the map's spatial reference, I got a null error.
Thanks.
-Rainer
Lucas,
I got it. I wasn't adding the spatial reference when I was adding graphics to the graphics layer. I assumed that when a graphic is added to a graphic layer that the graphic was set to the spatial reference of the graphic layer which it inherits from the bas map (tile package). I guess that was a bad assumption on my part.
-Rainer
Cool, glad you figured it out. You're correct, it does not automatically inherit the spatial reference once it is added to the GL.
-Luke