Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- var latlon, lat, lon, isYandex, zoom, map, x, y, ymax;
- function getLonByX(x, zoom) {
- try {
- return x / Math.pow(2, zoom) * 360 - 180;
- } catch(e) {}
- }
- function getLatByY(y, zoom) {
- try {
- var n = Math.PI - 2 * Math.PI * y / Math.pow(2, zoom);
- return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
- } catch(e) {}
- }
- function goByUrl(url) {
- try {
- url = decodeURIComponent(url);
- } catch(e) {}
- // Common style
- lat = (lat = /(?:lat|latitude|fLat|\Wlt)[^\d-]+([\d.-]*)/.exec(url)) === null ? lat : lat[1];
- lon = (lon = /(?:lon|lng|long|longitude|fLon|\Wln)[^\d-]+([\d.-]*)/.exec(url)) === null ? lon : lon[1];
- zoom = (zoom = /(?:zoom|zl|\Wz)\D*([\d.]*)/.exec(url)) === null ? '14' : zoom[1];
- if (go()) return true;
- // Yandex and old Google style
- latlon = (latlon = /ll=([\d.,-]*)/.exec(url)) === null ? latlon : latlon[1].split(',');
- if (latlon && latlon.length > 1) {
- isYandex = /\/\/[^\/]*yandex/.test(url) | 0;
- lon = latlon[1 - isYandex];
- lat = latlon[isYandex];
- if (go()) return true;
- }
- // New Google style
- latlon = /@([\d.-]*),([\d.-]*),(\d+)z/.exec(url);
- if (latlon) {
- lat = latlon[1];
- lon = latlon[2];
- zoom = latlon[3];
- if (go()) return true;
- }
- // Openstreetmap new style
- latlon = /(\d+)\/(-?\d+\.\d+)\/(-?\d+\.\d+)/.exec(url);
- if (latlon) {
- lat = latlon[2];
- lon = latlon[3];
- zoom = latlon[1];
- if (go()) return true;
- }
- // 2Gis style
- latlon = /(-?\d+\.\d+),(-?\d+\.\d+)\/zoom\/(\d+)/.exec(url);
- if (latlon) {
- lat = latlon[2];
- lon = latlon[1];
- zoom = latlon[3];
- if (go()) return true;
- }
- // OSM/TMS tile
- latlon = /(\d+)\/(-?\d+)\/(-?\d+)/.exec(url);
- if (latlon) {
- zoom = parseInt(latlon[1]);
- y = parseInt(latlon[3]);
- x = parseInt(latlon[2]);
- lat = getLatByY(y, zoom);
- lon = getLonByX(x, zoom);
- go(true);
- // TMS URL
- ymax = 1 << zoom;
- y = ymax - y - 1;
- lat = getLatByY(y, zoom);
- lon = getLonByX(x, zoom);
- if (go()) return true;
- }
- // x/y tile
- x = /x=(\d+)/.exec(url);
- y = /y=(\d+)/.exec(url);
- zoom = /z=(\d+)/.exec(url);
- if (x !== null && y !== null && zoom !== null) {
- x = parseInt(x[1]);
- y = parseInt(y[1]);
- zoom = parseInt(zoom[1]);
- lat = getLatByY(y, zoom);
- lon = getLonByX(x, zoom);
- go(true);
- // TMS URL
- ymax = 1 << zoom;
- y = ymax - y - 1;
- lat = getLatByY(y, zoom);
- lon = getLonByX(x, zoom);
- }
- return go();
- }
- function goBySelectedUrls(selector, urlAttribute) {
- var elements = document.querySelectorAll(selector);
- var length = elements.length > 500 ? 500 : elements.length;
- for (var i = 0; i < length; i++)
- if (goByUrl(elements[i][urlAttribute])) return true;
- }
- function go(inNewWindow) {
- if (!(lat && lon)) return;
- var url = 'http://openstreetmap.ru/#map=' + zoom + '/' + lat + '/' + lon;
- if (inNewWindow)
- window.open(url);
- else
- window.location = url;
- return true;
- }
- if (goByUrl(window.location.href)) return;
- // Vkontakte
- map = window.placeMap || (window.cur ? window.cur.placeMap : null);
- if (map) {
- latlon = map.getCenter();
- lat = latlon.lat;
- lon = latlon.lon;
- zoom = map.getZoom();
- if (go()) return;
- }
- // Bing
- map = window.map;
- if (map && map.get_center && map.get_zoomLevel) {
- latlon = map.get_center();
- lon = latlon.longitude;
- lat = latlon.latitude;
- zoom = map.get_zoomLevel();
- if (go()) return;
- }
- // Try to extract from links on page
- if (goBySelectedUrls('a[href*="map"],a[href*="lat="],a[href*="lon="]', 'href')) return;
- if (goBySelectedUrls('img[src*="map"],img[src*="tile"]', 'src')) return;
- goBySelectedUrls('a[href]', 'href');
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement