none
Microsoft.Maps.SpatialMath.Geometry.calculateLength returning 0 RRS feed

  • Question

  • Hi I am trying to use the Microsoft.Maps.SpatialMath.Geometry.calculateLength method but it is always returning 0, not sure what am I doing wrong.

    Essentially I am capturing the events on the DrawingTools object:

    Microsoft.Maps.loadModule('Microsoft.Maps.DrawingTools', function () { _this.drawingTools = new Microsoft.Maps.DrawingTools(_this.map); Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingStarted', function (e) { _this.onDrawingToolsDrawingStarted(e);});

    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingModeChanged', function (e) { _this.onDrawingToolsDrawingModeChanged(e);});                    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingChanging', function (e) { _this.onDrawingToolsDrawingChanging(e);});

    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingChanged', function (e) { _this.onDrawingToolsDrawingChanged(e);});

    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingEnded', function (e) { _this.onDrawingToolsDrawingEnded(e); });

    });

        onDrawingToolsDrawingChanging: function (e) {
            this.measureShape(e.geometry);
        },

        measureShape: function (geometry) {
            if (this.measurementPanel) {
                var length = Microsoft.Maps.SpatialMath.Geometry.calculateLength(geometry, this.measurementUnits);

                //Round values to 1 decimals.
                length = Math.round(length * 10) / 10;

                if (this.measurementUnits == Microsoft.Maps.SpatialMath.DistanceUnits.Kilometers) {
                    this.measurementLabel.setHtml('Distance: ' + length + ' km');
                } else {
                    this.measurementLabel.setHtml('Distance: ' + length + ' mi');
                }
            }
        },

    The problem is that:

    var length = Microsoft.Maps.SpatialMath.Geometry.calculateLength(geometry, this.measurementUnits);

    Is always returning 0

    Sunday, January 14, 2018 12:49 AM

Answers

All replies

  • None of your events call the onDrawingToolsDrawingChanging function.

    You may also want to take a look at this code sample: 

    http://bingmapsv8samples.azurewebsites.net/#DrawingTools_MeasuringTape


    [Blog] [twitter] [LinkedIn]

    Sunday, January 14, 2018 3:57 AM
  • Hi thanks, that was just a copy/paste issue on my behalf. The initialization looks like this:

    Microsoft.Maps.loadModule('Microsoft.Maps.DrawingTools', function () {             

    _this.drawingTools = new Microsoft.Maps.DrawingTools(_this.map);                    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingStarted', function (e) { _this.onDrawingToolsDrawingStarted(e); });                    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingModeChanged', function (e) { _this.onDrawingToolsDrawingModeChanged(e); });                    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingChanging', function (e) { _this.onDrawingToolsDrawingChanging(e); });                    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingChanged', function (e) { _this.onDrawingToolsDrawingChanged(e); });                    Microsoft.Maps.Events.addHandler(_this.drawingTools, 'drawingEnded', function (e) { _this.onDrawingToolsDrawingEnded(e); });                });

    Microsoft.Maps.loadModule('Microsoft.Maps.SpatialMath', function () { });

    When I put a break point it is being hit and the calculateLength is being executed with no error or exceptions but the result is always 0.

    I did my code using an example very similar (although not exactly the same)

    Sunday, January 14, 2018 4:38 AM
  • Ah, see the issue. The value passed to the event callback (e) is your drawn shape. But your code is doing 3.geometry which is not right and likely undefined which when passed into the calculateLength function returns a value of 0 which is correct.

    [Blog] [twitter] [LinkedIn]

    • Proposed as answer by Ricky_Brundritt Sunday, January 14, 2018 6:05 AM
    • Marked as answer by fernando06 Sunday, January 14, 2018 9:22 AM
    Sunday, January 14, 2018 6:05 AM
  • Thanks! that was it (can't believe didn't check that)
    • Marked as answer by fernando06 Sunday, January 14, 2018 9:22 AM
    • Unmarked as answer by fernando06 Sunday, January 14, 2018 9:22 AM
    Sunday, January 14, 2018 9:21 AM