Advertisement
Guest User

Openstreetmapru bookmarklet sources

a guest
Jul 3rd, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.   var latlon, lat, lon, isYandex, zoom, map, x, y, ymax;
  3.  
  4.   function getLonByX(x, zoom) {
  5.     try {
  6.       return x / Math.pow(2, zoom) * 360 - 180;
  7.     } catch(e) {}
  8.   }
  9.  
  10.   function getLatByY(y, zoom) {
  11.     try {
  12.       var n = Math.PI - 2 * Math.PI * y / Math.pow(2, zoom);
  13.       return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
  14.     } catch(e) {}
  15.   }
  16.  
  17.   function goByUrl(url) {
  18.     try {
  19.       url = decodeURIComponent(url);
  20.     } catch(e) {}
  21.  
  22.     // Common style
  23.     lat = (lat = /(?:lat|latitude|fLat|\Wlt)[^\d-]+([\d.-]*)/.exec(url)) === null ? lat : lat[1];
  24.     lon = (lon = /(?:lon|lng|long|longitude|fLon|\Wln)[^\d-]+([\d.-]*)/.exec(url)) === null ? lon : lon[1];
  25.     zoom = (zoom = /(?:zoom|zl|\Wz)\D*([\d.]*)/.exec(url)) === null ? '14' : zoom[1];
  26.     if (go()) return true;
  27.  
  28.     // Yandex and old Google style
  29.     latlon = (latlon = /ll=([\d.,-]*)/.exec(url)) === null ? latlon : latlon[1].split(',');
  30.  
  31.     if (latlon && latlon.length > 1) {
  32.       isYandex = /\/\/[^\/]*yandex/.test(url) | 0;
  33.       lon = latlon[1 - isYandex];
  34.       lat = latlon[isYandex];
  35.       if (go()) return true;
  36.     }
  37.  
  38.     // New Google style
  39.     latlon = /@([\d.-]*),([\d.-]*),(\d+)z/.exec(url);
  40.     if (latlon) {
  41.       lat = latlon[1];
  42.       lon = latlon[2];
  43.       zoom = latlon[3];
  44.       if (go()) return true;
  45.     }
  46.  
  47.     // Openstreetmap new style
  48.     latlon = /(\d+)\/(-?\d+\.\d+)\/(-?\d+\.\d+)/.exec(url);
  49.     if (latlon) {
  50.       lat = latlon[2];
  51.       lon = latlon[3];
  52.       zoom = latlon[1];
  53.       if (go()) return true;
  54.     }
  55.  
  56.     // 2Gis style
  57.     latlon = /(-?\d+\.\d+),(-?\d+\.\d+)\/zoom\/(\d+)/.exec(url);
  58.     if (latlon) {
  59.       lat = latlon[2];
  60.       lon = latlon[1];
  61.       zoom = latlon[3];
  62.       if (go()) return true;
  63.     }
  64.  
  65.     // OSM/TMS tile
  66.     latlon = /(\d+)\/(-?\d+)\/(-?\d+)/.exec(url);
  67.     if (latlon) {
  68.       zoom = parseInt(latlon[1]);
  69.       y = parseInt(latlon[3]);
  70.       x = parseInt(latlon[2]);
  71.       lat = getLatByY(y, zoom);
  72.       lon = getLonByX(x, zoom);
  73.       go(true);
  74.  
  75.       // TMS URL
  76.       ymax = 1 << zoom;
  77.       y = ymax - y - 1;
  78.       lat = getLatByY(y, zoom);
  79.       lon = getLonByX(x, zoom);
  80.       if (go()) return true;
  81.     }
  82.  
  83.     // x/y tile
  84.     x = /x=(\d+)/.exec(url);
  85.     y = /y=(\d+)/.exec(url);
  86.     zoom = /z=(\d+)/.exec(url);
  87.     if (x !== null && y !== null && zoom !== null) {
  88.       x = parseInt(x[1]);
  89.       y = parseInt(y[1]);
  90.       zoom = parseInt(zoom[1]);
  91.       lat = getLatByY(y, zoom);
  92.       lon = getLonByX(x, zoom);
  93.       go(true);
  94.  
  95.       // TMS URL
  96.       ymax = 1 << zoom;
  97.       y = ymax - y - 1;
  98.       lat = getLatByY(y, zoom);
  99.       lon = getLonByX(x, zoom);
  100.     }
  101.  
  102.     return go();
  103.   }
  104.  
  105.   function goBySelectedUrls(selector, urlAttribute) {
  106.     var elements = document.querySelectorAll(selector);
  107.     var length = elements.length > 500 ? 500 : elements.length;
  108.     for (var i = 0; i < length; i++)
  109.       if (goByUrl(elements[i][urlAttribute])) return true;
  110.   }
  111.  
  112.   function go(inNewWindow) {
  113.     if (!(lat && lon)) return;
  114.  
  115.     var url = 'http://openstreetmap.ru/#map=' + zoom + '/' + lat + '/' + lon;
  116.  
  117.     if (inNewWindow)
  118.       window.open(url);
  119.     else
  120.       window.location = url;
  121.     return true;
  122.   }
  123.  
  124.   if (goByUrl(window.location.href)) return;
  125.  
  126.   // Vkontakte
  127.   map = window.placeMap || (window.cur ? window.cur.placeMap : null);
  128.   if (map) {
  129.     latlon = map.getCenter();
  130.     lat = latlon.lat;
  131.     lon = latlon.lon;
  132.     zoom = map.getZoom();
  133.     if (go()) return;
  134.   }
  135.  
  136.   // Bing
  137.   map = window.map;
  138.   if (map && map.get_center && map.get_zoomLevel) {
  139.     latlon = map.get_center();
  140.     lon = latlon.longitude;
  141.     lat = latlon.latitude;
  142.     zoom = map.get_zoomLevel();
  143.     if (go()) return;
  144.   }
  145.  
  146.   // Try to extract from links on page
  147.   if (goBySelectedUrls('a[href*="map"],a[href*="lat="],a[href*="lon="]', 'href')) return;
  148.   if (goBySelectedUrls('img[src*="map"],img[src*="tile"]', 'src')) return;
  149.   goBySelectedUrls('a[href]', 'href');
  150. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement