v2 Change: Map Bounding Lat/Long

In v1 of the standard control, if you needed to get the bounding lat/long of the map you could do this:

var y1 = map.GetLatitude(0);
var x1 = map.GetLongitude(0);    
var y2 = map.GetLatitude(map.height);
var x2 = map.GetLongitude(map.width);

There were two problems with this approach. For the South East corner it relies on map.width and height, which no longer exist in the v2 control (map width and height are governed by the container now, hence that easier initialization and resize).  But, you could now do this and it would work :

var c = document.getElementById(‘mapcontainer’);
var y1 = map.GetLatitude(0);
var x1 = map.GetLongitude(0);
var y2 = map.GetLatitude(c.offsetheight);
var x2 = map.GetLongitude(c.offsetwidth);

Also note, c.style.width will not work as it returns a string something like ’700px’, so you need to use the ‘offsetheight’ property.  So, this will get you working again, but is not an ideal solution.

The bigger issue is that map.GetLatitude() and map.GetLongitude() are deprecated in favor of new methods. The SDK documents PixelToLatLong() which should be used in their place; but, I don’t see a mention of the deprecation issue. If you are doing a simple line-for-line migration you may have simply missed this other method and find yourself stuck trying to figure out what to do with old getlatitude() based code when it is dropped later.  The solution, moving to PixelToLatLong() [or LatLongToPixel() for the opposite effect], is a slightly monger block of code, but works better for views like bird’s eye and is more object oriented.  Here is an example to replace the above sample: 

var c = document.getElementById(“mapcontainer”);
      
var UpperLeftPixel = new Msn.VE.Pixel();
UpperLeftPixel.x = 0;
UpperLeftPixel.y = 0;
UpperLeftLatLong = map.PixelToLatLong(UpperLeftPixel, map.GetZoomLevel());

var LowerRightPixel = new Msn.VE.Pixel();
LowerRightPixel.x = c.offsetWidth;
LowerRightPixel.y = c.offsetHeight;
LowerRightLatLong = map.PixelToLatLong(LowerRightPixel, map.GetZoomLevel());

So, a conversion to using the ‘offsetwidth’ property of the map container will get your application updated quickly.  But, ideally, you should try to move directly to the replacement function: PixelToLatLong().

Alex Daley
Technical Evangelist

Join the conversation

2 comments
  1. Anonymous

    I recently tried the code for v3 of the mapcontrol, but i always receive following error ..

    "Invalid argument; input argument ‘x’ is not a valid ‘int’ value."

    from "VEValidator.ValidateInt"

    when i call "map.PixelToLatLong(..)" .

    Any suggestions?

    Thanks,

    stephan

Comments are closed.