none
Bug Found? Bing Maps: tryPixelToLocation is drawing in reverse RRS feed

  • Question

  • We believe we found a bug here converting code from v7 to v8.  Any insight will be greatly appreciated.  The Complete code is below - but we are having two issues - one - the initial drawing placement is off.  2. when you mouse from north to south - or vicecersa, the drawing travels in the opposite direction. 

    Same code works with a slightly modified (for v7) if the v7 script is included instead

    <script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?branch=release&callback=loadMapScenario' ></script>
    
    <script type="text/javascript" language="javascript">
        var map, isDrawing, line, start, stop, move;  
    
          function startDrawing(e) {
                try{        
                    var point = new Microsoft.Maps.Point(e.getX(), e.getY());
                    var loc = e.target.tryPixelToLocation(point);
                    line = new Microsoft.Maps.Polyline([loc, loc]);
    
                    map.entities.push(line);
    
                    isDrawing = true;
                    map.setOptions({disablePanning: true, disableZooming: true});
                }catch(x){}
          }
    
          function mouseMoved(e) {
            if(isDrawing){
                try{
                    var point = new Microsoft.Maps.Point(e.getX(), e.getY());
                    var loc = e.target.tryPixelToLocation(point);
                    var locs = line.getLocations();
                    locs.push(loc);
                    line.setLocations(locs);
                }catch(x){}
            }
          }
    
          function stopDrawing() {
              isDrawing = false;
              map.setOptions({disablePanning: false, disableZooming: false});
              closeDrawing();
          }
    
          function closeDrawing() {
                var thispolyline = line.getLocations();
                var thispolylinecount = thispolyline.length - 1
    
                var startLoc = new Microsoft.Maps.Location(thispolyline[0].latitude, thispolyline[0].longitude);
                var endLoc = new Microsoft.Maps.Location(thispolyline[ thispolylinecount ].latitude, thispolyline[ thispolylinecount ].longitude);
                var lineVertices = new Array(startLoc, endLoc);
                var closeLine = new Microsoft.Maps.Polyline(lineVertices);
    
                map.entities.push(closeLine);
    
                Microsoft.Maps.Events.removeHandler(start);
                Microsoft.Maps.Events.removeHandler(stop);
                Microsoft.Maps.Events.removeHandler(move);
    
                var points = new Array()
                points = thispolyline
    
                var polygoncolor = new Microsoft.Maps.Color(2,85,36,72);
                var polygon  = new Microsoft.Maps.Polygon(points, {fillColor: polygoncolor, strokeColor: polygoncolor});
    
                map.entities.push(polygon)
                map.setView({bounds: Microsoft.Maps.LocationRect.fromLocations(points)});
        }
    
    
        function loadMapScenario() {
            map = new Microsoft.Maps.Map(document.getElementById('map_canvas'), {
                credentials: 'Your Credentials',
                zoom:13,
                mapTypeId: Microsoft.Maps.MapTypeId.road
            });
    
             Microsoft.Maps.Events.addHandler(map, 'mousedown', startDrawing);
             Microsoft.Maps.Events.addHandler(map, 'mouseup', stopDrawing);
             Microsoft.Maps.Events.addHandler(map, 'mousemove', mouseMoved);
    
    
            map.setOptions({ minZoom: 8, zoom:13});     
        }
    
    
    </script>
        <div id="map_canvas" class="map_canvas" style="width: 100%; height: 400px;" ></div>
    

    Friday, August 5, 2016 9:56 PM

Answers

  • Yup, looks like a bug. Surprised no one else has mentioned this as its been like this for a while. I'll let the team know.

    That said, all the work you are doing to convert the mouse point to a location isn't needed in V8 anymore. The event argument now has a point and location properties which are Point and Location objects. To update your code, change this:

    var point = new Microsoft.Maps.Point(e.getX(), e.getY());
    var loc = e.target.tryPixelToLocation(point);

    To this:

    var loc = e.location;


    [Blog] [twitter] [LinkedIn]

    Friday, August 5, 2016 11:29 PM
  • This will likely be fixed in the experimental branch of V8 in the next week or so. Fixes in the experimental branch are usually pushed into the main release branch by the end of the first week of the following month, in this case around the end of the first week of September.

    That said, if you use e.location instead of your V7 code, it would not only simplify your code but would also make it slightly faster as you wouldn't need to do any pixel/location calculations.


    [Blog] [twitter] [LinkedIn]


    Sunday, August 7, 2016 9:40 PM

All replies

  • Yup, looks like a bug. Surprised no one else has mentioned this as its been like this for a while. I'll let the team know.

    That said, all the work you are doing to convert the mouse point to a location isn't needed in V8 anymore. The event argument now has a point and location properties which are Point and Location objects. To update your code, change this:

    var point = new Microsoft.Maps.Point(e.getX(), e.getY());
    var loc = e.target.tryPixelToLocation(point);

    To this:

    var loc = e.location;


    [Blog] [twitter] [LinkedIn]

    Friday, August 5, 2016 11:29 PM
  • Thank you!  I'll revert to v7 code until resolved.  If your team remembers to respond to this post it would be much appreciated!


    Goliath Development

    Saturday, August 6, 2016 2:41 AM
  • This will likely be fixed in the experimental branch of V8 in the next week or so. Fixes in the experimental branch are usually pushed into the main release branch by the end of the first week of the following month, in this case around the end of the first week of September.

    That said, if you use e.location instead of your V7 code, it would not only simplify your code but would also make it slightly faster as you wouldn't need to do any pixel/location calculations.


    [Blog] [twitter] [LinkedIn]


    Sunday, August 7, 2016 9:40 PM
  • Good news, this is now fixed in the experimental branch of V8 and will be in the main release branch in a couple of weeks. Thanks for reporting.

    [Blog] [twitter] [LinkedIn]

    Friday, August 26, 2016 5:08 PM
  • Awesome - I will test away.  I'm was awaiting the release to mark the issue as answered / complete.

    Many Thanks.


    Goliath Development

    Friday, August 26, 2016 5:56 PM