Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 3.64 KB  |  views: 66  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to correctly get the bounding box using LocationRect.fromLocations() when locations span 180th meridian?
  2. var map, MM = Microsoft.Maps;
  3.  
  4. function showMap(m) {
  5.   var options = {
  6.     mapTypeId: MM.MapTypeId.road // aerial,
  7.     // center will be recalculated
  8.     // zoom will be recalculated
  9.   },
  10.   map1 = new MM.Map(m, options);
  11.   return map1;
  12. }
  13.  
  14. function doubleclickCallback(e) {
  15.   e.handled = true;
  16.   var bounds = map.getBounds();
  17.   map.setView({ bounds: bounds });
  18. }
  19.  
  20. function init() {
  21.   var mapDiv = document.getElementById("map1");
  22.     map = showMap(mapDiv);
  23.  
  24.   MM.Events.addHandler(map, "dblclick", doubleclickCallback);
  25. }
  26.        
  27. <script charset="UTF-8" type="text/javascript"
  28.         src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0">
  29. </script>
  30.        
  31. function monkeyPatchMapMath() {
  32.   Microsoft.Maps.InternalNamespaceForDelay.MapMath.
  33.     locationRectToMercatorZoom = function (windowDimensions, bounds) {
  34.       var ins = Microsoft.Maps.InternalNamespaceForDelay,
  35.         d = windowDimensions,
  36.         g = Microsoft.Maps.Globals,
  37.         n = bounds.getNorth(),
  38.         s = bounds.getSouth(),
  39.         e = bounds.getEast(),
  40.         w = bounds.getWest(),
  41.         f = ((e+360 - w) % 360)/360,
  42.         //f = Math.abs(w - e) / 360,
  43.         u = Math.abs(ins.MercatorCube.latitudeToY(n) -
  44.                      ins.MercatorCube.latitudeToY(s)),
  45.         r = Math.min(d.width / (g.zoomOriginWidth * f),
  46.                      d.height / (g.zoomOriginWidth * u));
  47.       return ins.VectorMath.log2(r);
  48.     };
  49. }
  50.  
  51.  
  52.  
  53. function monkeyPatchFromLocations() {
  54.   Microsoft.Maps.LocationRect.fromLocations = function () {
  55.     var com = Microsoft.Maps.InternalNamespaceForDelay.Common,
  56.       o = com.isArray(arguments[0]) ? arguments[0] : arguments,
  57.       latMax, latMin, lngMin1, lngMin2, lngMax1, lngMax2, c,
  58.       lngMin, lngMax, LL, dx1, dx2,
  59.       pt = Microsoft.Maps.AltitudeReference,
  60.       s, e, n, f = o.length;
  61.  
  62.     while (f--)
  63.       n = o[f],
  64.     isFinite(n.latitude) && isFinite(n.longitude) &&
  65.       (latMax = latMax === c ? n.latitude : Math.max(latMax, n.latitude),
  66.        latMin = latMin === c ? n.latitude : Math.min(latMin, n.latitude),
  67.        lngMax1 = lngMax1 === c ? n.longitude : Math.max(lngMax1, n.longitude),
  68.        lngMin1 = lngMin1 === c ? n.longitude : Math.min(lngMin1, n.longitude),
  69.        LL = n.longitude,
  70.        (LL < 0) && (LL += 360),
  71.        lngMax2 = lngMax2 === c ? LL : Math.max(lngMax2, LL),
  72.        lngMin2 = lngMin2 === c ? LL : Math.min(lngMin2, LL),
  73.        isFinite(n.altitude) && pt.isValid(n.altitudeReference) &&
  74.        (e = n.altitude, s = n.altitudeReference));
  75.  
  76.     dx1 = lngMax1 - lngMin1,
  77.     dx2 = lngMax2 - lngMin2,
  78.     lngMax = (dx1 > dx2) ? lngMax2 : lngMax1,
  79.     lngMin = (dx1 > dx2) ? lngMin2 : lngMin1;
  80.  
  81.     return MM.LocationRect.fromEdges(latMax, lngMin, latMin, lngMax, e, s);
  82.   };
  83. }
  84.        
  85. monkeyPatchFromLocations();
  86. bounds = Microsoft.Maps.LocationRect.fromLocations(allPoints);
  87. monkeyPatchMapMath();
  88. map1.setView({bounds:bounds});
  89.        
  90. bounds = Microsoft.Maps.LocationRect.fromLocations(allPoints);
  91.        
  92. var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(polygon.getLocations());
  93.  
  94.                       map.setView({ bounds: viewBoundaries });
  95.                       map.setView({ zoom: 10 });
  96.        
  97. function doubleclickCallback(e) {
  98.   e.handled = true;
  99.   var bounds = map.getBounds();
  100.   map.setView({ bounds: bounds });
  101. }
  102.  
  103. MM.Events.addHandler(map, "dblclick", doubleclickCallback);
  104.        
  105. Microsoft.Maps.Events.addHandler(polygon, 'click', doubleclickCallback);
  106.        
  107. function doubleclickCallback(e) {
  108.   // Now we are getting the boundaries of our polygon
  109.   var bounds = e.target.getLocations();
  110.   map.setView({ bounds: bounds });
  111.   map.setView({ zoom: 9});
  112. }