Geoguessr Unity Script

Play Geoguessr with Yandex, Baidu, Kakao, Mapillary streetviews, Aris coverage (with good movement), and avoid the gaps in official coverage. Credit to kommu, MrAmericanMike (Yandex) and xsanda (check location after movement) scripts. Thanks to Alok, Mapper for advise for map making.

As of 2021-12-29. See the latest version.

  1. // ==UserScript==
  2. // @name Geoguessr Unity Script
  3. // @description Play Geoguessr with Yandex, Baidu, Kakao, Mapillary streetviews, Aris coverage (with good movement), and avoid the gaps in official coverage. Credit to kommu, MrAmericanMike (Yandex) and xsanda (check location after movement) scripts. Thanks to Alok, Mapper for advise for map making.
  4. // @version 4.1.0
  5. // @include https://www.geoguessr.com/*
  6. // @run-at document-start
  7. // @license MIT
  8. // @namespace https://greatest.deepsurf.us/users/838374
  9. // ==/UserScript==
  10.  
  11.  
  12. // API Keys
  13.  
  14. var YANDEX_API_KEY = "b704b5a9-3d67-4d19-b702-ec7807cecfc6";
  15. var BAIDU_API_KEY = "8dQ9hZPGEQnqg9r0O1C8Ate2N6P8Zk92";
  16. var KAKAO_API_KEY = "cbacbe41e3a223d794f321de4f3e247b";
  17. const MAPS_API_URL = "https://maps.googleapis.com/maps/api/js"; // removed "?" from the link
  18. var MAPILLARY_API_KEY_LIST =
  19. ["MLY|6723031704435203|5afd537469b114cf814881137ad74b7c",
  20. "MLY|6691659414239148|b45e7e82cde126044cbc2cf5d4a7c9b1",
  21. "MLY|5074369465929308|f7ad2802cbaf26c63f88046a292df68b",
  22. "MLY|7451643761528219|6477f2db0e3928b51e45ec9311983936",
  23. "MLY|4855256237866198|6d0464771831c8a4bf2be095e1e1aabc",
  24. "MLY|4772941976102161|8458d4f08d2e1970cdfe0a4e242c04ff",
  25. "MLY|4492067214235489|94c44703942362ad6f6b70b5d32c3a45",
  26. "MLY|4618251611628426|0cef71d6ec8b997a5ec06ecdeabf11ec",
  27. "MLY|4096846270415982|fa2ce29641503e6ef665f17459633570",
  28. "MLY|4231415756962414|fe353880fd246e8a4a6ae32152f7dbb0",]
  29.  
  30. var MAPILLARY_API_KEY = MAPILLARY_API_KEY_LIST[Math.floor(Math.random() * MAPILLARY_API_KEY_LIST.length)];
  31.  
  32. myLog("Geoguessr Unity Script V4.1.0");
  33. // myLog(MAPILLARY_API_KEY);
  34.  
  35. // Store each player instance
  36.  
  37. let YandexPlayer, BaiduPlayer, KakaoPlayer, GooglePlayer, MapillaryPlayer;
  38. let YANDEX_INJECTED = false;
  39. let BAIDU_INJECTED = false;
  40. let KAKAO_INJECTED = false;
  41. let MAPILLARY_INJECTED = false;
  42.  
  43. // Game mode detection
  44.  
  45. let isBattleRoyale = false;
  46. let isDuel = false;
  47.  
  48. // Player detection and coordinate conversion
  49.  
  50. let nextPlayer = "Google";
  51. let global_lat = 0;
  52. let global_lng = 0;
  53. let global_panoID = null;
  54. let global_heading = null;
  55. let global_pitch = null;
  56.  
  57. let krCoordinates = [38.75292321084364, 124.2804539232574, 33.18509676203202, 129.597381999198]
  58. let global_radi = 100
  59.  
  60. // Callback variables
  61.  
  62. let eventListenerAttached = false;
  63. let povListenerAttached = false;
  64. let playerLoaded = false;
  65. let teleportLoaded = false;
  66. let syncLoaded = false;
  67.  
  68. // Minimize Yandex API use
  69.  
  70. let yandex_map = false;
  71.  
  72. // Mapillary Image Key
  73.  
  74. let mmKey = 0;
  75.  
  76. // Handle Yandex compass
  77.  
  78. let COMPASS = null;
  79.  
  80. // Handle undo
  81.  
  82. let locHistory = [];
  83. let defaultPanoIdChange = true;
  84.  
  85. // Round check
  86.  
  87. let ROUND = 0;
  88. let CURRENT_ROUND_DATA = null;
  89.  
  90. let switch_call = true;
  91. let one_reset = false;
  92. // let cnt = 0;
  93.  
  94. // let NEW_ROUND_LOADED = false;
  95.  
  96. /**
  97. * Helper Functions
  98. */
  99.  
  100. // Pretty print
  101.  
  102. function myLog(...args) {
  103. console.log(...args);
  104. }
  105. function myHighlight(...args) {
  106. console.log(`%c${[...args]}`, "color: dodgerblue; font-size: 24px;");
  107. }
  108.  
  109. // Hex to number conversion for Baidu coordinate conversion
  110.  
  111. function hex2a(hexx) {
  112. var hex = hexx.toString();
  113. var str = '';
  114. for (var i = 0; i < hex.length; i += 2)
  115. str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
  116. return str;
  117. }
  118.  
  119. // Coordinate computation given heading, distance and current coordinates for teleport
  120.  
  121. function FindPointAtDistanceFrom(lat, lng, initialBearingRadians, distanceKilometres) {
  122. const radiusEarthKilometres = 6371.01;
  123. var distRatio = distanceKilometres / radiusEarthKilometres;
  124. var distRatioSine = Math.sin(distRatio);
  125. var distRatioCosine = Math.cos(distRatio);
  126.  
  127. var startLatRad = DegreesToRadians(lat);
  128. var startLonRad = DegreesToRadians(lng);
  129.  
  130. var startLatCos = Math.cos(startLatRad);
  131. var startLatSin = Math.sin(startLatRad);
  132.  
  133. var endLatRads = Math.asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.cos(initialBearingRadians)));
  134.  
  135. var endLonRads = startLonRad
  136. + Math.atan2(
  137. Math.sin(initialBearingRadians) * distRatioSine * startLatCos,
  138. distRatioCosine - startLatSin * Math.sin(endLatRads));
  139.  
  140. return { lat: RadiansToDegrees(endLatRads), lng: RadiansToDegrees(endLonRads) };
  141. }
  142.  
  143. function DegreesToRadians(degrees) {
  144. const degToRadFactor = Math.PI / 180;
  145. return degrees * degToRadFactor;
  146. }
  147.  
  148. function RadiansToDegrees(radians) {
  149. const radToDegFactor = 180 / Math.PI;
  150. return radians * radToDegFactor;
  151. }
  152.  
  153. // Check if two floating point numbers are really really really really close to each other (to 10 decimal points)
  154. function almostEqual (a, b) {
  155. return a.toFixed(10) === b.toFixed(10)
  156. }
  157.  
  158. function almostEqual2 (a, b) {
  159. return a.toFixed(3) === b.toFixed(3)
  160. }
  161.  
  162. // Script injection, extracted from extenssr:
  163. // https://gitlab.com/nonreviad/extenssr/-/blob/main/src/injected_scripts/maps_api_injecter.ts
  164.  
  165. function overrideOnLoad(googleScript, observer, overrider) {
  166. const oldOnload = googleScript.onload
  167. googleScript.onload = (event) => {
  168. const google = unsafeWindow.google
  169. if (google) {
  170. observer.disconnect()
  171. overrider(google)
  172. }
  173. if (oldOnload) {
  174. oldOnload.call(googleScript, event)
  175. }
  176. }
  177. }
  178.  
  179. function grabGoogleScript(mutations) {
  180. for (const mutation of mutations) {
  181. for (const newNode of mutation.addedNodes) {
  182. const asScript = newNode
  183. if (asScript && asScript.src && asScript.src.startsWith('https://maps.googleapis.com/')) {
  184. return asScript
  185. }
  186. }
  187. }
  188. return null
  189. }
  190.  
  191. function injecter(overrider) {
  192. new MutationObserver((mutations, observer) => {
  193. const googleScript = grabGoogleScript(mutations)
  194. if (googleScript) {
  195. overrideOnLoad(googleScript, observer, overrider)
  196. }
  197. }).observe(document.documentElement, { childList: true, subtree: true })
  198. }
  199.  
  200. /**
  201. * Creates teleportation and Kakao switch button
  202. *
  203. * @returns Promise
  204. */
  205.  
  206. function ArisKakao() {
  207. // let radi = 100;
  208. const google = unsafeWindow.google;
  209. // console.log(google);
  210. let curPosition;
  211. let kakao_enabled = true;
  212.  
  213. // Helper Functions
  214.  
  215. function FindPointAtDistanceFrom(startPoint, initialBearingRadians, distanceKilometres) {
  216. const radiusEarthKilometres = 6371.01;
  217. var distRatio = distanceKilometres / radiusEarthKilometres;
  218. var distRatioSine = Math.sin(distRatio);
  219. var distRatioCosine = Math.cos(distRatio);
  220.  
  221. var startLatRad = DegreesToRadians(startPoint.lat);
  222. var startLonRad = DegreesToRadians(startPoint.lng);
  223.  
  224. var startLatCos = Math.cos(startLatRad);
  225. var startLatSin = Math.sin(startLatRad);
  226.  
  227. var endLatRads = Math.asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.cos(initialBearingRadians)));
  228.  
  229. var endLonRads = startLonRad
  230. + Math.atan2(
  231. Math.sin(initialBearingRadians) * distRatioSine * startLatCos,
  232. distRatioCosine - startLatSin * Math.sin(endLatRads));
  233.  
  234. return { lat: RadiansToDegrees(endLatRads), lng: RadiansToDegrees(endLonRads) };
  235. }
  236.  
  237. function svCheck(data, status) {
  238. if (status === 'OK') {
  239. // console.log("OK for " + data.location.latLng + " at ID " + data.location.pano);
  240. let l = data.location.latLng.toString().split(',');
  241. let lat = l[0].replaceAll('(', '')
  242. let lng = l[1].replaceAll(')', '')
  243. if (lat == curPosition.lat && lng == curPosition.lng && !switch_call)
  244. {
  245. console.log("Trying more distance");
  246. teleportButton.distance += 100;
  247. teleportButton.innerHTML = "Teleport " + teleportButton.distance + " m";
  248. }
  249. else
  250. {
  251. console.log("Teleport Success");
  252. GooglePlayer.setPosition(data.location.latLng);
  253. GooglePlayer.setPov({
  254. heading: googleKakaoButton.heading,
  255. pitch: 0,
  256. })
  257. // console.log(teleportButton.distance);
  258. if (teleportButton.distance > 150)
  259. {
  260. teleportButton.distance = 100;
  261. teleportButton.innerHTML = "Teleport " + teleportButton.distance + " m";
  262. }
  263. }
  264. switch_call = false;
  265. }
  266. else {
  267. console.log("STATUS NOT OK");
  268. teleportButton.distance += 100;
  269. teleportButton.innerHTML = "Teleport " + teleportButton.distance + " m";
  270. }
  271. }
  272.  
  273. const svService = new google.maps.StreetViewService();
  274. google.maps.StreetViewPanorama = class extends google.maps.StreetViewPanorama {
  275. constructor(...args) {
  276. super(...args);
  277. GooglePlayer = this;
  278.  
  279. const isGamePage = () => location.pathname.startsWith("/challenge/") || location.pathname.startsWith("/results/") || location.pathname.startsWith("/game/")|| location.pathname.startsWith("/battle-royale/") || location.pathname.startsWith("/duels/") || location.pathname.startsWith("/team-duels/");
  280.  
  281. this.addListener('position_changed', () => {
  282. // Maybe this could be used to update the position in the other players
  283. // so that they are always in sync
  284. try {
  285. if (!isGamePage()) return;
  286. // YearButton.panoId = GooglePlayer.pano;
  287. // YearButton.index = -1;
  288. const lat = this.getPosition().lat();
  289. const lng = this.getPosition().lng();
  290. const { heading } = this.getPov();
  291.  
  292. curPosition = { lat, lng, heading };
  293.  
  294. if (googleKakaoButton.useGoogle)
  295. {
  296. googleKakaoButton.lng = lng;
  297. googleKakaoButton.lat = lat;
  298. googleKakaoButton.heading = heading;
  299. if (!YearButton.list.some(row => row.includes(GooglePlayer.pano)))
  300. {
  301. YearButton.innerHTML = "Time Machine";
  302. YearButton.panoId = GooglePlayer.pano;
  303. YearButton.index = -1;
  304. YearButton.plusminusLock = true;
  305. document.getElementById("plus year").style.backgroundColor = "red";
  306. document.getElementById("plus year").disabled = true;
  307. document.getElementById("minus year").style.backgroundColor = "red";
  308. document.getElementById("minus year").disabled = true;
  309. }
  310. }
  311. googleKakaoButton.useGoogle = true;
  312. teleportButton.google = true;
  313. // console.log("also run");
  314.  
  315. // googleKakaoButton.heading = position.lat;
  316. // console.log(position.heading);
  317. // console.log(googleKakaoButton.lng);
  318. }
  319. catch (e) {
  320. console.error("GeoGuessr Path Logger Error:", e);
  321. }
  322. });
  323. this.addListener('pov_changed', () => {
  324. const { heading, pitch } = this.getPov();
  325. if (KakaoPlayer) {
  326. const vp = KakaoPlayer.getViewpoint();
  327. // Prevent a recursive loop: only update kakao's viewpoint if it got out of sync with google's
  328. if (!almostEqual(vp.pan, heading) || !almostEqual(vp.tilt, pitch)) {
  329. KakaoPlayer.setViewpoint({ pan: heading, tilt: pitch, zoom: vp.zoom });
  330. }
  331. }
  332. });
  333. }
  334. };
  335.  
  336.  
  337. var showButtons = document.createElement("Button");
  338. showButtons.id = "Show Buttons"
  339. showButtons.innerHTML = "Geoguessr Unity Script <font size=1>by Jupaoqq</font>";
  340. showButtons.style =
  341. "top:6em;right:0.5em;width:6em;height:7em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  342. document.body.appendChild(showButtons);
  343. showButtons.addEventListener("click", () => {
  344. if (hide) {
  345. teleportButton.style.visibility = "";
  346. plusButton.style.visibility = "";
  347. minusButton.style.visibility = "";
  348. resetButton.style.visibility = "";
  349. googleKakaoButton.style.visibility = "";
  350. YearButton.style.visibility = "";
  351. YearplusButton.style.visibility = "";
  352. YearminusButton.style.visibility = "";
  353. hide = false;
  354. }
  355. else {
  356. teleportButton.style.visibility = "hidden";
  357. plusButton.style.visibility = "hidden";
  358. minusButton.style.visibility = "hidden";
  359. resetButton.style.visibility = "hidden"
  360. googleKakaoButton.style.visibility = "hidden";
  361. YearButton.style.visibility = "hidden";
  362. YearplusButton.style.visibility = "hidden";
  363. YearminusButton.style.visibility = "hidden";
  364. hide = true;
  365. }
  366. });
  367.  
  368. var teleportButton = document.createElement("Button");
  369. teleportButton.id = "Main Button";
  370. teleportButton.distance = 100;
  371. teleportButton.google = true;
  372. teleportButton.innerHTML = "Teleport 100m";
  373. teleportButton.style =
  374. "visibility:hidden;top:6em;right:9.5em;width:10em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  375. document.body.appendChild(teleportButton);
  376. teleportButton.addEventListener("click", () => {
  377. // console.log("Google Teleport");
  378. if (googleKakaoButton.init)
  379. {
  380. // console.log("run");
  381. googleKakaoButton.init = false;
  382. if (teleportButton.google)
  383. {
  384. googleKakaoButton.useGoogle = true;
  385. teleportButton.google = true;
  386. }
  387. else
  388. {
  389. googleKakaoButton.useGoogle = false;
  390. teleportButton.google = false;
  391. }
  392. }
  393. else
  394. {
  395. // myLog(teleportButton.google)
  396. if (teleportButton.google && GooglePlayer != null)
  397. {
  398. let heading = GooglePlayer.getPov().heading;
  399. let place = FindPointAtDistanceFrom(curPosition, DegreesToRadians(heading), teleportButton.distance * 0.001)
  400. svService.getPanorama({ location: place, radius: 1000 }, svCheck);
  401. }
  402. }
  403. });
  404.  
  405. var plusButton = document.createElement("Button");
  406. plusButton.id = "plus"
  407. plusButton.innerHTML = "+";
  408. plusButton.style =
  409. "visibility:hidden;top:6em;right:7em;width:2em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  410. document.body.appendChild(plusButton);
  411. plusButton.addEventListener("click", () => {
  412. if (teleportButton.distance > 21 && teleportButton.distance < 149) {
  413. teleportButton.distance = teleportButton.distance + 25;
  414. }
  415. teleportButton.innerHTML = "Teleport " + teleportButton.distance + " m";
  416. });
  417.  
  418. var minusButton = document.createElement("Button");
  419. minusButton.id = "minus"
  420. minusButton.innerHTML = "-";
  421. minusButton.style =
  422. "visibility:hidden;top:6em;right:20em;width:2em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  423. document.body.appendChild(minusButton);
  424. minusButton.addEventListener("click", () => {
  425. if (teleportButton.distance > 26) {
  426. teleportButton.distance = teleportButton.distance - 25;
  427. }
  428. teleportButton.innerHTML = "Teleport " + teleportButton.distance + " m";
  429. });
  430.  
  431. var resetButton = document.createElement("Button");
  432. resetButton.id = "reset"
  433. resetButton.innerHTML = "Reset";
  434. resetButton.style =
  435. "visibility:hidden;top:8.5em;right:17.5em;width:4.5em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  436. document.body.appendChild(resetButton);
  437. resetButton.addEventListener("click", () => {
  438. teleportButton.distance = 100;
  439. teleportButton.innerHTML = "Teleport " + teleportButton.distance + " m";
  440. });
  441.  
  442. var googleKakaoButton = document.createElement("Button");
  443. googleKakaoButton.id = "switch";
  444. googleKakaoButton.init = false;
  445. googleKakaoButton.nextPlayer = "Google";
  446. googleKakaoButton.useGoogle = false;
  447. googleKakaoButton.lng = 0
  448. googleKakaoButton.lat = 0
  449. googleKakaoButton.heading = 0
  450. googleKakaoButton.innerHTML = "Switch Coverage";
  451. googleKakaoButton.small_canvas = false;
  452. googleKakaoButton.style =
  453. "visibility:hidden;top:8.5em;right:7em;width:10em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  454. document.body.appendChild(googleKakaoButton);
  455. googleKakaoButton.addEventListener("click", () => {
  456. let GOOGLE_MAPS_CANVAS1 = document.querySelector(".game-layout__panorama-canvas");
  457. let GOOGLE_MAPS_CANVAS2 = document.querySelector(".br-game-layout__panorama-canvas");
  458. let GOOGLE_MAPS_CANVAS3 = document.querySelector(".inactive");
  459. let duel = false;
  460.  
  461. let GOOGLE_MAPS_CANVAS = null;
  462. if (GOOGLE_MAPS_CANVAS1 !== null)
  463. {
  464. GOOGLE_MAPS_CANVAS = GOOGLE_MAPS_CANVAS1;
  465. }
  466. else if (GOOGLE_MAPS_CANVAS2 !== null)
  467. {
  468. GOOGLE_MAPS_CANVAS = GOOGLE_MAPS_CANVAS2;
  469. }
  470.  
  471.  
  472. if (GOOGLE_MAPS_CANVAS3 !== null)
  473. {
  474. duel = true;
  475. }
  476.  
  477. let KAKAO_MAPS_CANVAS = document.getElementById("roadview");
  478. let YANDEX_MAPS_CANVAS = document.querySelector(".ymaps-2-1-79-panorama-screen");
  479. let MAPILLARY_MAPS_CANVAS = document.getElementById("mly")
  480. if (googleKakaoButton.nextPlayer !== "Baidu") {
  481. if (googleKakaoButton.useGoogle == false) {
  482. if (duel)
  483. {
  484. document.getElementById("default_player").className = "game-panorama_panoramaCanvas__patp9";
  485. if (googleKakaoButton.nextPlayer == "Kakao")
  486. {
  487. document.getElementById("roadview").className = "inactive";
  488. }
  489. else
  490. {
  491. MAPILLARY_MAPS_CANVAS.className = "inactive";
  492. MAPILLARY_MAPS_CANVAS.style.visibility = "hidden";
  493. }
  494. }
  495. else
  496. {
  497. GOOGLE_MAPS_CANVAS.style.visibility = "";
  498. if (googleKakaoButton.nextPlayer == "Kakao")
  499. {
  500. KAKAO_MAPS_CANVAS.style.visibility = "hidden";
  501. }
  502. else if (googleKakaoButton.nextPlayer == "Yandex")
  503. {
  504. YANDEX_MAPS_CANVAS.style.visibility = "hidden";
  505. }
  506. else if (googleKakaoButton.nextPlayer == "Mapillary" || googleKakaoButton.nextPlayer == "Google")
  507. {
  508. MAPILLARY_MAPS_CANVAS.style.visibility = "hidden";
  509. }
  510.  
  511. }
  512. const lat = GooglePlayer.getPosition().lat();
  513. const lng = GooglePlayer.getPosition().lng();
  514. switch_call = true;
  515. if (!almostEqual2(lat, googleKakaoButton.lat) || !almostEqual2(lat, googleKakaoButton.lng)) {
  516. svService.getPanorama({ location: { lat: googleKakaoButton.lat, lng: googleKakaoButton.lng }, radius: 1000 }, svCheck);
  517. }
  518. googleKakaoButton.useGoogle = true;
  519. teleportButton.google = true;
  520. googleKakaoButton.init = false;
  521. console.log("use Google");
  522. }
  523. else {
  524. if (duel)
  525. {
  526. document.getElementById("default_player").className = "inactive";
  527. if (googleKakaoButton.nextPlayer == "Kakao")
  528. {
  529. document.getElementById("roadview").className = "game-panorama_panorama__3b2wI";
  530. }
  531. else
  532. {
  533. MAPILLARY_MAPS_CANVAS.className = "game-panorama_panorama__3b2wI";
  534. MAPILLARY_MAPS_CANVAS.style.visibility = "";
  535. MapillaryPlayer.resize();
  536. //window.dispatchEvent(new Event('resize'));
  537. // document.querySelector(".mapillary-canvas").style.;
  538. // mapillary-canvas
  539. }
  540.  
  541. }
  542. else
  543. {
  544. GOOGLE_MAPS_CANVAS.style.visibility = "hidden";
  545. if (googleKakaoButton.nextPlayer == "Kakao")
  546. {
  547. KAKAO_MAPS_CANVAS.style.visibility = "";
  548. }
  549. else if (googleKakaoButton.nextPlayer == "Yandex")
  550. {
  551. YANDEX_MAPS_CANVAS.style.visibility = "";
  552. }
  553. else if (googleKakaoButton.nextPlayer == "Mapillary" || googleKakaoButton.nextPlayer == "Google")
  554. {
  555. MAPILLARY_MAPS_CANVAS.style.visibility = "";
  556. }
  557. }
  558. googleKakaoButton.useGoogle = false;
  559. teleportButton.google = false;
  560. googleKakaoButton.init = true;
  561. console.log("use Others");
  562. }
  563. }
  564. else {
  565. googleKakaoButton.useGoogle = false;
  566. teleportButton.google = false;
  567. console.log("use Others");
  568. }
  569.  
  570. });
  571.  
  572. // function reportWindowSize() {
  573. // myLog("Resize")
  574. // svService.getPanorama({ location: { lat: googleKakaoButton.lat, lng: googleKakaoButton.lng }, radius: 1000 }, svCheck);
  575. // }
  576.  
  577. // window.onresize = reportWindowSize;
  578.  
  579. var YearplusButton = document.createElement("Button");
  580. YearplusButton.id = "plus year"
  581. YearplusButton.innerHTML = "+";
  582. YearplusButton.style =
  583. "visibility:hidden;top:11em;right:7em;width:2em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  584. document.body.appendChild(YearplusButton);
  585. YearplusButton.addEventListener("click", () => {
  586. if (YearButton.index < YearButton.list.length - 1 && !YearButton.plusminusLock) {
  587. YearButton.index = YearButton.index + 1;
  588. GooglePlayer.setPano(YearButton.list[YearButton.index][0]);
  589. YearButton.innerHTML = "<font size=2>[" + (YearButton.index + 1) + "] " + YearButton.list[YearButton.index][1] + "</font>";
  590. // myLog(YearButton.index)
  591. }
  592. GenBtnColor();
  593.  
  594. });
  595.  
  596. var YearminusButton = document.createElement("Button");
  597. YearminusButton.id = "minus year"
  598. YearminusButton.innerHTML = "-";
  599. YearminusButton.style =
  600. "visibility:hidden;top:11em;right:20em;width:2em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  601. document.body.appendChild(YearminusButton);
  602. YearminusButton.addEventListener("click", () => {
  603. if (YearButton.index > 0 && !YearButton.plusminusLock) {
  604. YearButton.index = YearButton.index - 1;
  605. GooglePlayer.setPano(YearButton.list[YearButton.index][0]);
  606. YearButton.innerHTML = "<font size=2>[" + (YearButton.index + 1) + "] " + YearButton.list[YearButton.index][1] + "</font>";
  607. // myLog(YearButton.index)
  608. }
  609. GenBtnColor();
  610. });
  611.  
  612. function svCheck2(data, status) {
  613. let l = []
  614. if (status === 'OK') {
  615. // console.log("OK for " + data.location.latLng + " at ID " + data.location.pano);
  616. // myLog(data.time)
  617. for (const alt of data.time) {
  618. let date = Object.values(alt).find((value) => value instanceof Date)
  619.  
  620. l.push([alt.pano, date.toDateString()]);
  621. }
  622. // myLog(l);
  623. YearButton.list = l
  624. YearButton.index = l.length - 1;
  625. YearButton.innerHTML = "<font size=2>[" + (YearButton.index + 1) + "] " + YearButton.list[YearButton.index][1] + "</font>";
  626. GenBtnColor();
  627. YearButton.plusminusLock = false;
  628. // YearminusButton.click()
  629. // YearButton.innerHTML = "Default Date";
  630. }
  631. }
  632.  
  633. var YearButton = document.createElement("Button");
  634. YearButton.id = "Date Button";
  635. YearButton.plusminusLock = true;
  636. YearButton.panoId = 0;
  637. YearButton.index = -1;
  638. YearButton.list = [];
  639. YearButton.innerHTML = "Time Machine";
  640. YearButton.style =
  641. "visibility:hidden;top:11em;right:9.5em;width:10em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  642. document.body.appendChild(YearButton);
  643. YearButton.addEventListener("click", () => {
  644. // myLog(YearButton.index)
  645. if (YearButton.panoId != 0)
  646. {
  647. if(YearButton.index == -1)
  648. {
  649. svService.getPanorama({pano: YearButton.panoId}, svCheck2);
  650. }
  651. else
  652. {
  653. YearButton.index = YearButton.list.length - 1;
  654. GooglePlayer.setPano(YearButton.list[YearButton.index][0]);
  655. YearButton.innerHTML = "<font size=2>[" + (YearButton.index + 1) + "] " + YearButton.list[YearButton.index][1] + "</font>";
  656. GenBtnColor();
  657. }
  658. }
  659. else
  660. {
  661. YearButton.panoId = GooglePlayer.pano;
  662. svService.getPanorama({pano: YearButton.panoId}, svCheck2);
  663. }
  664. });
  665.  
  666. // Battle Royale UI optimization
  667.  
  668. let hide = true;
  669.  
  670. console.log("Buttons Loaded");
  671. }
  672.  
  673. function GenBtnColor()
  674. {
  675. if (document.getElementById("Date Button").index == document.getElementById("Date Button").list.length - 1)
  676. {
  677. document.getElementById("plus year").style.backgroundColor = "red";
  678. document.getElementById("plus year").disabled = true;
  679. }
  680. else
  681. {
  682. document.getElementById("plus year").style.backgroundColor = "#4CAF50";
  683. document.getElementById("plus year").disabled = false;
  684. }
  685. if (document.getElementById("Date Button").index == 0)
  686. {
  687. document.getElementById("minus year").style.backgroundColor = "red";
  688. document.getElementById("minus year").disabled = true;
  689. }
  690. else
  691. {
  692. document.getElementById("minus year").style.backgroundColor = "#4CAF50";
  693. document.getElementById("minus year").disabled = false;
  694. }
  695. }
  696.  
  697. /**
  698. * Handle Keyboard inputs
  699. */
  700.  
  701. function kBoard()
  702. {
  703. document.addEventListener('keydown', logKey);
  704. }
  705.  
  706. function logKey(e) {
  707. // myLog(e.code);
  708. if (e.code == "Space")
  709. {
  710. setHidden(true);
  711. }
  712. if (e.code == "Digit1")
  713. {
  714. setHidden(false);
  715. document.getElementById("Show Buttons").click();
  716. }
  717. else if (e.code == "Digit3")
  718. {
  719. document.getElementById("Main Button").click();
  720. }
  721. else if (e.code == "Digit2")
  722. {
  723. document.getElementById("minus").click();
  724. }
  725. else if (e.code == "Digit4")
  726. {
  727. document.getElementById("plus").click();
  728.  
  729. }
  730. else if (e.code == "Digit5")
  731. {
  732. document.getElementById("reset").click();
  733.  
  734. }
  735. else if (e.code == "Digit6")
  736. {
  737. document.getElementById("switch").click();
  738. }
  739. else if (e.code == "Digit7")
  740. {
  741. document.getElementById("minus year").click();
  742. }
  743. else if (e.code == "Digit8")
  744. {
  745. document.getElementById("Date Button").click();
  746. }
  747. else if (e.code == "Digit9")
  748. {
  749. document.getElementById("plus year").click();
  750. }
  751. }
  752.  
  753.  
  754. /**
  755. * Hide or reveal the buttons, and disable buttons if such feature is not available
  756. */
  757.  
  758. function setHidden(cond)
  759. {
  760. if (cond)
  761. {
  762. if (document.getElementById("Show Buttons") != null)
  763. {
  764. document.getElementById("Show Buttons").style.visibility = "hidden";
  765. if (document.getElementById("Main Button") != null)
  766. {
  767. document.getElementById("plus").style.visibility = "hidden";
  768. document.getElementById("minus").style.visibility = "hidden";
  769. document.getElementById("reset").style.visibility = "hidden";
  770. document.getElementById("Main Button").style.visibility = "hidden";
  771. document.getElementById("switch").style.visibility = "hidden";
  772. document.getElementById("Date Button").style.visibility = "hidden";
  773. document.getElementById("plus year").style.visibility = "hidden";
  774. document.getElementById("minus year").style.visibility = "hidden";
  775. }
  776. }
  777. }
  778. else
  779. {
  780. if (document.getElementById("Show Buttons") != null)
  781. {
  782. document.getElementById("Show Buttons").style.visibility = "";
  783. }
  784. }
  785. }
  786.  
  787. function setDisable(cond) {
  788. if (document.getElementById("Main Button") != null) {
  789. if (cond == "NMPZ") {
  790. document.getElementById("plus").style.backgroundColor = "red";
  791. document.getElementById("plus").disabled = true;
  792. document.getElementById("minus").style.backgroundColor = "red";
  793. document.getElementById("minus").disabled = true;
  794. document.getElementById("reset").style.backgroundColor = "red";
  795. document.getElementById("reset").disabled = true;
  796. if (nextPlayer == "Kakao")
  797. {
  798. document.getElementById("switch").style.backgroundColor = "#4CAF50";
  799. document.getElementById("switch").disabled = false;
  800. }
  801. else
  802. {
  803. document.getElementById("switch").style.backgroundColor = "red";
  804. document.getElementById("switch").disabled = true;
  805. }
  806. document.getElementById("switch").innerHTML = "Switch Coverage";
  807. document.getElementById("Main Button").disabled = true;
  808. document.getElementById("Main Button").style.backgroundColor = "red";
  809.  
  810. document.getElementById("Date Button").style.backgroundColor = "#4CAF50";
  811. document.getElementById("Date Button").disabled = false;
  812. }
  813. else if (cond == "Google") {
  814.  
  815. document.getElementById("plus").style.backgroundColor = "#4CAF50";
  816. document.getElementById("plus").disabled = false;
  817. document.getElementById("minus").style.backgroundColor = "#4CAF50";
  818. document.getElementById("minus").disabled = false;
  819. document.getElementById("reset").style.backgroundColor = "#4CAF50";
  820. document.getElementById("reset").disabled = false;
  821. document.getElementById("switch").style.backgroundColor = "#4CAF50";
  822. document.getElementById("switch").disabled = false;
  823. document.getElementById("switch").innerHTML = "Switch Coverage";
  824. document.getElementById("Main Button").disabled = false;
  825. document.getElementById("Main Button").style.backgroundColor = "#4CAF50";
  826. document.getElementById("Date Button").style.backgroundColor = "#4CAF50";
  827. document.getElementById("Date Button").disabled = false;
  828. }
  829. else if (cond == "Baidu") {
  830. document.getElementById("plus").style.backgroundColor = "#4CAF50";
  831. document.getElementById("plus").disabled = false;
  832. document.getElementById("minus").style.backgroundColor = "#4CAF50";
  833. document.getElementById("minus").disabled = false;
  834. document.getElementById("reset").style.backgroundColor = "#4CAF50";
  835. document.getElementById("reset").disabled = false;
  836. document.getElementById("switch").style.backgroundColor = "#4CAF50";
  837. document.getElementById("switch").disabled = false;
  838. if (document.getElementById("switch").small_canvas) {
  839. document.getElementById("switch").innerHTML = "Widescreen";
  840. }
  841. else {
  842. document.getElementById("switch").innerHTML = "More Viewing Area";
  843. }
  844. document.getElementById("Main Button").disabled = false;
  845. document.getElementById("Main Button").style.backgroundColor = "#4CAF50";
  846. document.getElementById("Date Button").style.backgroundColor = "red";
  847. document.getElementById("Date Button").disabled = true;
  848. }
  849. else if (cond == "Kakao" || cond == "Yandex" || cond == "Mapillary") {
  850. document.getElementById("plus").style.backgroundColor = "#4CAF50";
  851. document.getElementById("plus").disabled = false;
  852. document.getElementById("minus").style.backgroundColor = "#4CAF50";
  853. document.getElementById("minus").disabled = false;
  854. document.getElementById("reset").style.backgroundColor = "#4CAF50";
  855. document.getElementById("reset").disabled = false;
  856. document.getElementById("switch").style.backgroundColor = "#4CAF50";
  857. document.getElementById("switch").disabled = false;
  858. document.getElementById("switch").innerHTML = "Switch Coverage";
  859. document.getElementById("Main Button").disabled = false;
  860. document.getElementById("Main Button").style.backgroundColor = "#4CAF50";
  861. document.getElementById("Date Button").style.backgroundColor = "red";
  862. document.getElementById("Date Button").disabled = true;
  863. }
  864. document.getElementById("plus year").style.backgroundColor = "red";
  865. document.getElementById("plus year").disabled = true;
  866. document.getElementById("minus year").style.backgroundColor = "red";
  867. document.getElementById("minus year").disabled = true;
  868. // else if (cond == "Mapillary") {
  869. // document.getElementById("plus").style.backgroundColor = "red";
  870. // document.getElementById("plus").disabled = true;
  871. // document.getElementById("minus").style.backgroundColor = "red";
  872. // document.getElementById("minus").disabled = true;
  873. // document.getElementById("reset").style.backgroundColor = "red";
  874. // document.getElementById("reset").disabled = true;
  875. // document.getElementById("switch").style.backgroundColor = "#4CAF50";
  876. // document.getElementById("switch").disabled = false
  877. // document.getElementById("switch").innerHTML = "Switch Coverage";
  878. // document.getElementById("Main Button").disabled = true;
  879. // document.getElementById("Main Button").style.backgroundColor = "red";
  880. // }
  881.  
  882. }
  883. }
  884.  
  885.  
  886. /**
  887. * This observer stays alive while the script is running
  888. */
  889.  
  890. function launchObserver() {
  891. ArisKakao();
  892. BYKTeleport();
  893. SyncListener();
  894. kBoard();
  895. myHighlight("Main Observer");
  896. const OBSERVER = new MutationObserver((mutations, observer) => {
  897. detectGamePage();
  898. });
  899. OBSERVER.observe(document.head, { attributes: true, childList: true, subtree: true });
  900. }
  901.  
  902. /**
  903. * Once the Google Maps API was loaded we can do more stuff
  904. */
  905.  
  906. injecter(() => {
  907. launchObserver();
  908. })
  909.  
  910.  
  911. /**
  912. * Check whether the current page is a game, if so which game mode
  913. */
  914.  
  915. function detectGamePage() {
  916. if (document.querySelector(".game-layout__panorama-message") !== null && !one_reset)
  917. {
  918. one_reset = true;
  919. myLog("Hide fail to load panorama canvas");
  920. document.querySelector(".game-layout__panorama-message").style.visibility = "hidden";
  921. }
  922. let toLoad = !playerLoaded && !YandexPlayer && !BaiduPlayer && !KakaoPlayer && !MapillaryPlayer && !YANDEX_INJECTED && !BAIDU_INJECTED && !KAKAO_INJECTED && !MAPILLARY_INJECTED
  923. const PATHNAME = window.location.pathname;
  924. if (PATHNAME.startsWith("/game/") || PATHNAME.startsWith("/challenge/")) {
  925. // myLog("Game page");
  926. isBattleRoyale = false;
  927. isDuel = false;
  928. if (toLoad) {
  929. loadPlayers();
  930. }
  931. waitLoad();
  932. }
  933. else if (PATHNAME.startsWith("/battle-royale/")) {
  934. if (document.querySelector(".br-game-layout") == null) {
  935. // myLog("Battle Royale Lobby");
  936. rstValues();
  937. }
  938. else {
  939. // myLog("Battle Royale");
  940. isBattleRoyale = true;
  941. isDuel = false;
  942. if (toLoad) {
  943. loadPlayers();
  944. }
  945. waitLoad();
  946. }
  947. }
  948. else if (PATHNAME.startsWith("/duels/") || PATHNAME.startsWith("/team-duels/")) {
  949. if (document.querySelector(".game_layout__1TqBM") == null) {
  950. // myLog("Battle Royale Lobby");
  951. rstValues();
  952. }
  953. else {
  954. // myLog("Duels");
  955. isBattleRoyale = true;
  956. isDuel = true;
  957. if (toLoad) {
  958. loadPlayers();
  959. }
  960. waitLoad();
  961. }
  962. }
  963. else {
  964. rstValues();
  965. //myLog("Not a Game page");
  966. }
  967. }
  968.  
  969. function rstValues()
  970. {
  971. ROUND = 0;
  972. YandexPlayer = null;
  973. BaiduPlayer = null;
  974. KakaoPlayer = null;
  975. MapillaryPlayer = null;
  976.  
  977. BAIDU_INJECTED = false;
  978. YANDEX_INJECTED = false;
  979. KAKAO_INJECTED = false;
  980. MAPILLARY_INJECTED = false;
  981.  
  982. nextPlayer = "Google"
  983. global_lat = 0;
  984. global_lng = 0;
  985. global_panoID = null;
  986.  
  987. COMPASS = null;
  988. eventListenerAttached = false;
  989. povListenerAttached = false;
  990. playerLoaded = false;
  991. locHistory = [];
  992. one_reset = false;
  993. setHidden(true);
  994. yandex_map = false;
  995. mmKey = 0;
  996. CURRENT_ROUND_DATA = null;
  997. }
  998.  
  999. /**
  1000. * Wait for various players to load
  1001. */
  1002.  
  1003. function waitLoad() {
  1004. if (!YandexPlayer || !BaiduPlayer || !KakaoPlayer || !MapillaryPlayer || !YANDEX_INJECTED || !BAIDU_INJECTED || !KAKAO_INJECTED || !MAPILLARY_INJECTED) {
  1005. let teleportButton = document.getElementById("Main Button");
  1006. let plusButton = document.getElementById("plus");
  1007. let minusButton = document.getElementById("minus");
  1008. let resetButton = document.getElementById("reset");
  1009. let googleKakaoButton = document.getElementById("switch");
  1010. let showButtons = document.getElementById("Show Buttons");
  1011. let YearButton = document.getElementById("Date Button");
  1012. let YearminusButton = document.getElementById("minus year");
  1013. let YearplusButton = document.getElementById("plus year");
  1014. if (document.querySelector(".br-game-layout__panorama-canvas") != null)
  1015. {
  1016. teleportButton.style.top = "2px";
  1017. plusButton.style.top = "2px";
  1018. minusButton.style.top = "2px";
  1019. resetButton.style.top = "calc(2.5em + 2px)";
  1020. googleKakaoButton.style.top = "calc(2.5em + 2px)";
  1021. showButtons.style.top = "2px";
  1022. YearButton.style.top = "calc(5em + 2px)";
  1023. YearminusButton.style.top = "calc(5em + 2px)";
  1024. YearplusButton.style.top = "calc(5em + 2px)";
  1025.  
  1026. teleportButton.style.right = "calc(9.5em + 300px)";
  1027. plusButton.style.right = "calc(7em + 300px)";
  1028. minusButton.style.right = "calc(20em + 300px)";
  1029. resetButton.style.right = "calc(17.5em + 300px)";
  1030. googleKakaoButton.style.right = "calc(7em + 300px)";
  1031. showButtons.style.right = "300px";
  1032. YearButton.style.right = "calc(9.5em + 300px)";
  1033. YearminusButton.style.right = "calc(20em + 300px)";
  1034. YearplusButton.style.right = "calc(7em + 300px)";
  1035. }
  1036.  
  1037.  
  1038. if (document.querySelector(".game-panorama_panorama__3b2wI") != null)
  1039. {
  1040. teleportButton.style.top = "8em";
  1041. plusButton.style.top = "8em";
  1042. minusButton.style.top = "8em";
  1043. resetButton.style.top = "10.5em";
  1044. googleKakaoButton.style.top = "10.5em";
  1045. showButtons.style.top = "8em";
  1046. YearButton.style.top = "13em";
  1047. YearminusButton.style.top = "13em";
  1048. YearplusButton.style.top = "13em";
  1049.  
  1050. teleportButton.style.right = "9.5em";
  1051. plusButton.style.right = "7em";
  1052. minusButton.style.right = "20em";
  1053. resetButton.style.right = "17.5em";
  1054. googleKakaoButton.style.right = "7em";
  1055. showButtons.style.right = "0.5em";
  1056. YearButton.style.right = "9.5em";
  1057. YearminusButton.style.right = "20em";
  1058. YearplusButton.style.right = "7em";
  1059. }
  1060.  
  1061. setTimeout(waitLoad, 250);
  1062. } else {
  1063. checkRound();
  1064. }
  1065. }
  1066.  
  1067. /**
  1068. * Checks for round changes
  1069. */
  1070.  
  1071. function checkRound() {
  1072. // myLog("Check Round");
  1073. if (!isBattleRoyale) {
  1074. // myLog("Check Round");
  1075. let currentRound = getRoundFromPage();
  1076. if (ROUND != currentRound) {
  1077. document.getElementById("switch").init = true;
  1078. myHighlight("New round");
  1079. ROUND = currentRound;
  1080. // NEW_ROUND_LOADED = true;
  1081. COMPASS = null;
  1082. locHistory = [];
  1083. one_reset = false;
  1084. getMapData();
  1085. nextButtonCallback();
  1086. }
  1087. }
  1088. else {
  1089. getMapData();
  1090. }
  1091. }
  1092.  
  1093. /**
  1094. * Add listeners if buttons have been created
  1095. */
  1096.  
  1097. function nextButtonCallback()
  1098. {
  1099. let nextButton = document.querySelector("button[data-qa='close-round-result']");
  1100. if (nextButton != null)
  1101. {
  1102. nextButton.addEventListener("click", (e) => {
  1103. if (document.getElementById("Show Buttons") != null)
  1104. {
  1105. myLog("try to hide show buttons")
  1106. document.getElementById("Show Buttons").style.visibility = "";
  1107. }
  1108. })
  1109. }
  1110. else
  1111. {
  1112. setTimeout(nextButtonCallback, 500);
  1113. }
  1114. }
  1115.  
  1116. function guessButtonCallback()
  1117. {
  1118. let guessButton = document.querySelector("button[data-qa='perform-guess']");
  1119. if (guessButton != null)
  1120. {
  1121.  
  1122. guessButton.addEventListener("click", (e) => {
  1123. if (document.getElementById("Show Buttons") != null)
  1124. {
  1125. myLog("try to hide show buttons")
  1126. document.getElementById("Show Buttons").style.visibility = "hidden";
  1127. setHidden(true);
  1128. }
  1129. })
  1130. }
  1131. else
  1132. {
  1133. setTimeout(guessButtonCallback, 500);
  1134. }
  1135. }
  1136.  
  1137. /**
  1138. * Load different streetview players
  1139. */
  1140.  
  1141. function injectYandex()
  1142. {
  1143. injectYandexScript().then(() => {
  1144. myLog("Ready to inject Yandex player");
  1145. injectYandexPlayer();
  1146. }).catch((error) => {
  1147. myLog(error);
  1148. });
  1149. }
  1150.  
  1151. function loadPlayers() {
  1152. playerLoaded = true;
  1153. if (!isBattleRoyale)
  1154. {
  1155. getSeed().then((data) => {
  1156. // myLog(data);
  1157. if (data.mapName.includes("A United World") || data.mapName.includes("byk"))
  1158. {
  1159. myLog("A United World");
  1160. injectYandex();
  1161. }
  1162. else if (data.mapName.includes("Yandex"))
  1163. {
  1164. yandex_map = true;
  1165. myLog("Is Yandex Map");
  1166. injectYandex();
  1167. }
  1168. else{
  1169. // YANDEX_API_KEY = "";
  1170. YANDEX_INJECTED = true;
  1171. YandexPlayer = "YD";
  1172. myLog("Not a Yandex map");
  1173. }
  1174. setHidden(false);
  1175.  
  1176. }).catch((error) => {
  1177. myLog(error);
  1178. });
  1179. }
  1180. else
  1181. {
  1182. injectYandex();
  1183. }
  1184.  
  1185. initializeCanvas();
  1186.  
  1187.  
  1188.  
  1189.  
  1190. }
  1191.  
  1192. /**
  1193. * Handles Return to start and undo
  1194. */
  1195.  
  1196. function handleReturnToStart() {
  1197. let rtsButton = document.querySelector("button[data-qa='return-to-start']");
  1198. if (rtsButton != null) {
  1199. myLog("handleReturnToStart listener attached");
  1200. rtsButton.addEventListener("click", (e) => {
  1201. goToLocation();
  1202. const elementClicked = e.target;
  1203. elementClicked.setAttribute('listener', 'true');
  1204. myLog("Return to start");
  1205. });
  1206. guessButtonCallback();
  1207. setTimeout(function () {goToLocation();}, 1000);
  1208. }
  1209. else
  1210. {
  1211. setTimeout(handleReturnToStart, 500);
  1212. }
  1213. }
  1214.  
  1215. function handleUndo() {
  1216. let undoButton = document.querySelector("button[data-qa='undo-move']");
  1217. if (undoButton != null)
  1218. {
  1219. myLog("Attach undo");
  1220. undoButton.addEventListener("click", (e) => {
  1221. if (locHistory.length > 0) {
  1222. goToUndoMove();
  1223. myLog("Undo Move");
  1224. }
  1225. })
  1226. }
  1227. else
  1228. {
  1229. setTimeout(handleUndo, 500);
  1230. }
  1231.  
  1232. }
  1233.  
  1234. /**
  1235. * Load game information
  1236. */
  1237.  
  1238. function getMapData() {
  1239. // myHighlight("Seed data");
  1240. getSeed().then((data) => {
  1241. // myHighlight("Seed data");
  1242. // myLog(data);
  1243. if (isBattleRoyale) {
  1244. if ((document.querySelector(".br-game-layout") == null && document.querySelector(".version3-in-game_layout__13T8U") == null) || typeof data.gameId == typeof undefined) {
  1245. // myLog("Battle Royale Lobby");
  1246. }
  1247. else
  1248. {
  1249. let origin = false;
  1250. if (!CURRENT_ROUND_DATA) {
  1251. CURRENT_ROUND_DATA = data
  1252. origin = true;
  1253. }
  1254.  
  1255. if (origin || !(data.currentRoundNumber === CURRENT_ROUND_DATA.currentRoundNumber)) {
  1256. // myHighlight("Battle Royale New round");
  1257. document.getElementById("switch").init = true;
  1258. // NEW_ROUND_LOADED = true;
  1259. COMPASS = null;
  1260. locHistory = [];
  1261. one_reset = false;
  1262. setHidden(false);
  1263. if (!origin) {
  1264. CURRENT_ROUND_DATA = data;
  1265. }
  1266. locationCheck(data);
  1267. // myLog(data);
  1268. if (data.currentRoundNumber == 1)
  1269. {
  1270. setTimeout(function () {goToLocation();}, 3000);
  1271. }
  1272. else
  1273. {
  1274. goToLocation();
  1275. }
  1276. handleReturnToStart();
  1277. if (isDuel)
  1278. {
  1279. handleUndo();
  1280. hideButtons();
  1281. }
  1282.  
  1283. }
  1284. }
  1285. }
  1286. else {
  1287. locationCheck(data);
  1288. if (data.currentRoundNumber == 1)
  1289. {
  1290. setTimeout(function () {goToLocation();}, 3000);
  1291. }
  1292. else
  1293. {
  1294. goToLocation();
  1295. }
  1296. handleReturnToStart();
  1297. handleUndo();
  1298. hideButtons();
  1299. }
  1300. }).catch((error) => {
  1301. myLog(error);
  1302. });
  1303. }
  1304.  
  1305. /**
  1306. * Hide unnecessary buttons for non-Google coverages
  1307. */
  1308.  
  1309. function hideButtons() {
  1310. let CHECKPOINT = document.querySelector("button[data-qa='set-checkpoint']");
  1311. let ZOOM_IN = document.querySelector("button[data-qa='pano-zoom-in']");
  1312. let ZOOM_OUT = document.querySelector("button[data-qa='pano-zoom-out']");
  1313.  
  1314. if (CHECKPOINT != null)
  1315. {
  1316. if (nextPlayer === "Google") {
  1317.  
  1318. CHECKPOINT.style.visibility = "";
  1319. ZOOM_IN.style.visibility = "";
  1320. ZOOM_OUT.style.visibility = "";
  1321. myLog("Buttons Unhidden");
  1322.  
  1323. }
  1324. else {
  1325.  
  1326. CHECKPOINT.style.visibility = "hidden";
  1327. ZOOM_IN.style.visibility = "hidden";
  1328. ZOOM_OUT.style.visibility = "hidden";
  1329. myLog("Buttons Hidden");
  1330.  
  1331. }
  1332. }
  1333. else
  1334. {
  1335. setTimeout(hideButtons, 250);
  1336. }
  1337. }
  1338.  
  1339. /**
  1340. * Check which player to use for the next location
  1341. */
  1342.  
  1343. function locationCheck(data) {
  1344. // console.log(data);
  1345. let round;
  1346. if (isBattleRoyale) {
  1347. if (isDuel)
  1348. {
  1349. round = data.rounds[data.currentRoundNumber - 1].panorama;
  1350. }
  1351. else
  1352. {
  1353. round = data.rounds[data.currentRoundNumber - 1];
  1354. }
  1355. }
  1356. else {
  1357. round = data.rounds[data.round - 1];
  1358. }
  1359. global_lat = round.lat;
  1360. global_lng = round.lng;
  1361. global_panoID = round.panoId;
  1362. global_heading = round.heading;
  1363. global_pitch = round.pitch;
  1364. // myLog(global_lat);
  1365. // myLog(global_lng);
  1366. // myLog(krCoordinates);
  1367.  
  1368. nextPlayer = "Google";
  1369.  
  1370. if ( krCoordinates[0] > global_lat && krCoordinates[2] < global_lat && krCoordinates[1] < global_lng && krCoordinates[3] > global_lng)
  1371. {
  1372. nextPlayer = "Kakao";
  1373. }
  1374. else if (yandex_map)
  1375. {
  1376. nextPlayer = "Yandex";
  1377. }
  1378. else
  1379. {
  1380. if (global_panoID) {
  1381. let locInfo = hex2a(global_panoID);
  1382. let mapType = locInfo.substring(0, 5);
  1383. if (mapType === "BDMAP") {
  1384. nextPlayer = "Baidu";
  1385. let coord = locInfo.substring(5);
  1386. global_lat = coord.split(",")[0];
  1387. global_lng = coord.split(",")[1];
  1388. // myLog(global_lat);
  1389. }
  1390. else if (mapType === "MLMAP") {
  1391. nextPlayer = "Mapillary";
  1392. mmKey = locInfo.substring(5);
  1393. }
  1394. else if (mapType === "YDMAP" ) {
  1395. nextPlayer = "Yandex";
  1396. }
  1397. }
  1398. }
  1399.  
  1400. // Disable buttons if NM, NMPZ
  1401.  
  1402. if(!isBattleRoyale)
  1403. {
  1404. if (data.forbidMoving || data.forbidRotating || data.forbidZooming)
  1405. {
  1406. setDisable("NMPZ");
  1407. }
  1408. else
  1409. {
  1410. setDisable(nextPlayer);
  1411. }
  1412. }
  1413. else
  1414. {
  1415. if (data.movementOptions.forbidMoving || data.movementOptions.forbidRotating || data.movementOptions.forbidZooming)
  1416. {
  1417. setDisable("NMPZ");
  1418. }
  1419. else
  1420. {
  1421. setDisable(nextPlayer);
  1422. }
  1423. }
  1424.  
  1425. myLog(nextPlayer);
  1426. injectCanvas();
  1427. }
  1428.  
  1429.  
  1430. /**
  1431. * setID for canvas
  1432. */
  1433.  
  1434. function initializeCanvas() {
  1435. let GAME_CANVAS = "";
  1436. let DUEL_CANVAS = "";
  1437. //myLog("Is duels");
  1438. //myLog(duels);
  1439.  
  1440. if (isBattleRoyale) {
  1441. if (isDuel) {
  1442. GAME_CANVAS = document.querySelector(".game-panorama_panorama__3b2wI");
  1443. DUEL_CANVAS = document.querySelector(".game-panorama_panoramaCanvas__patp9");
  1444. }
  1445. else
  1446. {
  1447. GAME_CANVAS = document.querySelector(".br-game-layout__panorama-wrapper");
  1448. DUEL_CANVAS = "dummy";
  1449. }
  1450. }
  1451. else {
  1452. GAME_CANVAS = document.querySelector(".game-layout__canvas");
  1453. DUEL_CANVAS = "dummy";
  1454. }
  1455. if (GAME_CANVAS && DUEL_CANVAS)
  1456. {
  1457. myLog("Canvas injected");
  1458. GAME_CANVAS.id = "player";
  1459. if (isDuel) {
  1460. DUEL_CANVAS.id = "default_player";
  1461. }
  1462. injectBaiduPlayer();
  1463. injectMapillaryPlayer();
  1464. injectKakaoScript().then(() => {
  1465. myLog("Ready to inject Kakao player");
  1466. }).catch((error) => {
  1467. myLog(error);
  1468. });
  1469. }
  1470. else
  1471. {
  1472. setTimeout(initializeCanvas, 250);
  1473. }
  1474.  
  1475. }
  1476.  
  1477. /**
  1478. * Hide or show players based on where the next location is
  1479. */
  1480.  
  1481. function injectCanvas() {
  1482. if (isDuel)
  1483. {
  1484. canvasSwitch();
  1485. }
  1486. else
  1487. {
  1488. Google();
  1489. Baidu();
  1490. Kakao();
  1491. Yandex();
  1492. Mapillary();
  1493. }
  1494. ZoomControls();
  1495. }
  1496.  
  1497. // for duels (class ID change)
  1498.  
  1499. function canvasSwitch()
  1500. {
  1501. let GOOGLE_MAPS_CANVAS = document.querySelector(".game-panorama_panoramaCanvas__patp9");
  1502. document.querySelector(".ymaps-2-1-79-panorama-screen").style.position = "absolute";
  1503. if (nextPlayer === "Google") {
  1504. document.getElementById("default_player").className = "game-panorama_panoramaCanvas__patp9";
  1505. document.getElementById("PanoramaMap").className = "inactive";
  1506. document.getElementById("roadview").className = "inactive";
  1507. document.querySelector(".ymaps-2-1-79-panorama-screen").style.visibility = "hidden";
  1508. document.getElementById("Main Button").google = true;
  1509. document.getElementById("switch").nextPlayer = "Google";
  1510. document.getElementById("switch").useGoogle = true;
  1511. document.getElementById("default_player").style.position = "absolute";
  1512. if (document.querySelector(".compass") !== null)
  1513. {
  1514. document.querySelector(".compass").style.visibility = "";
  1515. }
  1516. myLog("Google Duel Canvas loaded");
  1517. }
  1518. else if (nextPlayer === "Baidu")
  1519. {
  1520. document.getElementById("default_player").className = "inactive";
  1521. document.getElementById("PanoramaMap").className = "game-panorama_panorama__3b2wI";
  1522. document.getElementById("roadview").className = "inactive";
  1523. document.getElementById("mly").style.visibility = "hidden";
  1524. document.querySelector(".ymaps-2-1-79-panorama-screen").style.visibility = "hidden";
  1525. document.getElementById("Main Button").google = false;
  1526. document.getElementById("switch").nextPlayer = "Baidu";
  1527. document.getElementById("switch").useGoogle = false;
  1528. document.getElementById("PanoramaMap").style.position = "absolute";
  1529. if (document.querySelector(".compass") !== null)
  1530. {
  1531. document.querySelector(".compass").style.visibility = "hidden";
  1532. }
  1533. myLog("Baidu Duel Canvas loaded");
  1534. }
  1535. else if (nextPlayer === "Kakao")
  1536. {
  1537. document.getElementById("default_player").className = "inactive";
  1538. document.getElementById("PanoramaMap").className = "inactive";
  1539. document.getElementById("roadview").className = "game-panorama_panorama__3b2wI";
  1540. document.getElementById("mly").style.visibility = "hidden";
  1541. document.querySelector(".ymaps-2-1-79-panorama-screen").style.visibility = "hidden";
  1542. document.getElementById("Main Button").google = false;
  1543. document.getElementById("switch").nextPlayer = "Kakao";
  1544. document.getElementById("switch").useGoogle = false;
  1545. document.getElementById("roadview").style.position = "absolute";
  1546. if (document.querySelector(".compass") !== null)
  1547. {
  1548. document.querySelector(".compass").style.visibility = "";
  1549. }
  1550. myLog("Kakao Duel Canvas loaded");
  1551. }
  1552. else if (nextPlayer === "Yandex")
  1553. {
  1554. document.getElementById("default_player").className = "inactive";
  1555. document.getElementById("PanoramaMap").className = "inactive";
  1556. document.getElementById("roadview").className = "inactive";
  1557. document.getElementById("mly").style.visibility = "hidden";
  1558. document.querySelector(".ymaps-2-1-79-panorama-screen").style.visibility = "";
  1559. document.getElementById("Main Button").google = false;
  1560. document.getElementById("switch").nextPlayer = "Yandex";
  1561. document.getElementById("switch").useGoogle = false;
  1562. document.querySelector(".ymaps-2-1-79-panorama-screen").style.position = "absolute";
  1563. if (document.querySelector(".compass") !== null)
  1564. {
  1565. document.querySelector(".compass").style.visibility = "";
  1566. }
  1567. myLog("Yandex Duel Canvas loaded");
  1568. }
  1569. else if (nextPlayer === "Mapillary")
  1570. {
  1571. document.getElementById("default_player").className = "inactive";
  1572. document.getElementById("PanoramaMap").className = "inactive";
  1573. document.getElementById("roadview").className = "inactive";
  1574. document.getElementById("mly").style.visibility = "";
  1575. document.querySelector(".ymaps-2-1-79-panorama-screen").style.visibility = "hidden";
  1576. document.getElementById("Main Button").google = false;
  1577. document.getElementById("switch").nextPlayer = "Mapillary";
  1578. document.getElementById("switch").useGoogle = false;
  1579. document.getElementById("mly").style.position = "absolute";
  1580. if (document.querySelector(".compass") !== null)
  1581. {
  1582. document.querySelector(".compass").style.visibility = "hidden";
  1583. }
  1584. myLog("Mapillary Duel Canvas loaded");
  1585. }
  1586. }
  1587.  
  1588. // for Battle Royale and classic (change visibility)
  1589.  
  1590. function Google() {
  1591. let GOOGLE_MAPS_CANVAS = ""
  1592. if (isBattleRoyale) {
  1593. GOOGLE_MAPS_CANVAS = document.querySelector(".br-game-layout__panorama-canvas");
  1594. }
  1595. else {
  1596. GOOGLE_MAPS_CANVAS = document.querySelector(".game-layout__panorama-canvas");
  1597. }
  1598. if (nextPlayer === "Google") {
  1599. GOOGLE_MAPS_CANVAS.style.visibility = "";
  1600. document.getElementById("Main Button").google = true;
  1601. document.getElementById("switch").nextPlayer = "Google";
  1602. document.getElementById("switch").useGoogle = true;
  1603. myLog("Google Canvas loaded");
  1604. }
  1605. else {
  1606. GOOGLE_MAPS_CANVAS.style.visibility = "hidden";
  1607. document.getElementById("Main Button").google = false;
  1608. myLog("Google Canvas hidden");
  1609. }
  1610.  
  1611. }
  1612.  
  1613. function Baidu() {
  1614. let BAIDU_MAPS_CANVAS = document.getElementById("PanoramaMap");
  1615. // myLog("Baidu canvas");
  1616. document.getElementById("PanoramaMap").style.position = "absolute";
  1617. if (BAIDU_MAPS_CANVAS != null)
  1618. {
  1619. if (nextPlayer === "Baidu") {
  1620. BAIDU_MAPS_CANVAS.style.visibility = "";
  1621. document.getElementById("switch").nextPlayer = "Baidu";
  1622. document.getElementById("switch").useGoogle = false;
  1623. if (document.querySelector(".compass") !== null)
  1624. {
  1625. document.querySelector(".compass").style.visibility = "hidden";
  1626. }
  1627. myLog("Baidu Canvas loaded");
  1628. }
  1629. else {
  1630. if (document.querySelector(".compass") !== null)
  1631. {
  1632. document.querySelector(".compass").style.visibility = "";
  1633. }
  1634. BAIDU_MAPS_CANVAS.style.visibility = "hidden";
  1635. myLog("Baidu Canvas hidden");
  1636. }
  1637. }
  1638. else
  1639. {
  1640. setTimeout(Baidu, 250);
  1641. }
  1642.  
  1643. }
  1644.  
  1645. function Kakao() {
  1646. let KAKAO_MAPS_CANVAS = document.getElementById("roadview");
  1647. // myLog("Kakao canvas");
  1648. document.getElementById("roadview").style.position = "absolute";
  1649. if (KAKAO_MAPS_CANVAS != null)
  1650. {
  1651. if (nextPlayer === "Kakao") {
  1652. KAKAO_MAPS_CANVAS.style.visibility = "";
  1653. document.getElementById("switch").nextPlayer = "Kakao";
  1654. document.getElementById("switch").useGoogle = false;
  1655. myLog("Kakao Canvas loaded");
  1656. }
  1657. else {
  1658. KAKAO_MAPS_CANVAS.style.visibility = "hidden";
  1659. myLog("Kakao Canvas hidden");
  1660. }
  1661. }
  1662. else
  1663. {
  1664. setTimeout(Kakao, 250);
  1665. }
  1666.  
  1667. }
  1668.  
  1669. function Yandex() {
  1670. let YANDEX_MAPS_CANVAS = document.querySelector(".ymaps-2-1-79-panorama-screen");
  1671. if (YANDEX_MAPS_CANVAS != null)
  1672. {
  1673. // myLog("Yandex canvas");
  1674. document.querySelector(".ymaps-2-1-79-panorama-screen").style.position = "absolute";
  1675. // myLog("Yandex canvas");
  1676. /* myLog(YANDEX_MAPS_CANVAS); */
  1677. if (nextPlayer === "Yandex") {
  1678. YANDEX_MAPS_CANVAS.style.visibility = "";
  1679. document.getElementById("switch").nextPlayer = "Yandex";
  1680. document.getElementById("switch").useGoogle = false;
  1681. myLog("Yandex Canvas loaded");
  1682. }
  1683. else {
  1684. YANDEX_MAPS_CANVAS.style.visibility = "hidden";
  1685. myLog("Yandex Canvas hidden");
  1686. }
  1687. }
  1688. else
  1689. {
  1690. setTimeout(Yandex, 250);
  1691. }
  1692.  
  1693. }
  1694.  
  1695. function Mapillary()
  1696. {
  1697.  
  1698. let MAPILLARY_MAPS_CANVAS = document.getElementById("mly");
  1699. if (MAPILLARY_MAPS_CANVAS != null)
  1700. {
  1701. // myLog("Mapillary canvas");
  1702. MAPILLARY_MAPS_CANVAS.style.position = "absolute";
  1703. if (nextPlayer === "Mapillary") {
  1704. MAPILLARY_MAPS_CANVAS.style.visibility = "";
  1705. document.getElementById("switch").nextPlayer = "Mapillary";
  1706. document.getElementById("switch").useGoogle = false;
  1707. myLog("Mapillary Canvas loaded");
  1708. }
  1709. else {
  1710. MAPILLARY_MAPS_CANVAS.style.visibility = "hidden";
  1711. myLog("Mapillary Canvas hidden");
  1712. }
  1713. }
  1714. else
  1715. {
  1716. setTimeout(Mapillary, 250);
  1717. }
  1718.  
  1719. }
  1720.  
  1721. /**
  1722. * Adjust button placement
  1723. */
  1724.  
  1725. function ZoomControls() {
  1726. let style = `
  1727. .ymaps-2-1-79-panorama-gotoymaps {display: none !important;}
  1728. .game-layout__controls {bottom: 8rem !important; left: 1rem !important;}
  1729. `;
  1730.  
  1731. let style_element = document.createElement("style");
  1732. style_element.innerHTML = style;
  1733. document.body.appendChild(style_element);
  1734. }
  1735.  
  1736. /**
  1737. * Updates the compass to match Yandex Panorama facing
  1738. */
  1739. function updateCompass() {
  1740. if (!COMPASS) {
  1741. let compass = document.querySelector("img.compass__indicator");
  1742. if (compass != null) {
  1743. COMPASS = compass;
  1744. let direction = YandexPlayer.getDirection()[0] * -1;
  1745. COMPASS.setAttribute("style", `transform: rotate(${direction}deg);`);
  1746. }
  1747. }
  1748. else {
  1749. let direction = YandexPlayer.getDirection()[0] * -1;
  1750. COMPASS.setAttribute("style", `transform: rotate(${direction}deg);`);
  1751. }
  1752. }
  1753.  
  1754. /**
  1755. * Open next location in streetview player given next player and next coordinate
  1756. */
  1757.  
  1758. function goToLocation() {
  1759. myLog("Going to location");
  1760. if (nextPlayer === "Yandex") {
  1761. let options = {};
  1762. YandexPlayer.moveTo([global_lat, global_lng], options);
  1763. YandexPlayer.setDirection([0, 16]);
  1764. YandexPlayer.setSpan([10, 67]);
  1765. }
  1766. else if (nextPlayer === "Baidu") {
  1767. let a = new BMap.Point(global_lng, global_lat);
  1768. BaiduPlayer.setPov({ heading: -40, pitch: 6 });
  1769. BaiduPlayer.setPosition(a);
  1770. }
  1771. else if (nextPlayer === "Kakao") {
  1772. var roadviewClient = new kakao.maps.RoadviewClient();
  1773. var position = new kakao.maps.LatLng(global_lat, global_lng);
  1774. roadviewClient.getNearestPanoId(position, 500, function (panoId) {
  1775. KakaoPlayer.setPanoId(panoId, position);
  1776. KakaoPlayer.setViewpoint({ pan: global_heading, tilt: global_pitch, zoom: 0 })
  1777. });
  1778. }
  1779. else if (nextPlayer === "Mapillary") {
  1780. MapillaryPlayer.moveTo(mmKey).then(
  1781. image => { //myLog(image);
  1782. },
  1783. error => { myLog(error); });
  1784. }
  1785. else if (nextPlayer === "Google") {
  1786. handleMapillary({lat: global_lat, lng: global_lng}, {meters: 500, limit: 500});
  1787. }
  1788. document.getElementById("switch").lat = global_lat;
  1789. document.getElementById("switch").lng = global_lng;
  1790. }
  1791.  
  1792. /**
  1793. * Handle undo using the location history of the current round
  1794. */
  1795.  
  1796. function goToUndoMove(data) {
  1797. /* myLog(global_lat);
  1798. myLog(global_lng); */
  1799. let options = {};
  1800. let prevStep = null;
  1801. if (locHistory.length === 1) {
  1802. prevStep = locHistory[0];
  1803. }
  1804. else {
  1805. prevStep = locHistory.pop();
  1806. }
  1807. // myLog(prevStep);
  1808. // myLog(locHistory)
  1809. if (nextPlayer === "Yandex") {
  1810. defaultPanoIdChange = false;
  1811. YandexPlayer.moveTo([prevStep[0], prevStep[1]], options);
  1812. YandexPlayer.setDirection([prevStep[2], prevStep[3]]);
  1813. YandexPlayer.setSpan([10, 67]);
  1814. document.getElementById("switch").lat = prevStep[0];
  1815. document.getElementById("switch").lng = prevStep[1];
  1816. }
  1817. else if (nextPlayer === "Kakao") {
  1818. let btn = document.querySelector("button[data-qa='undo-move']");
  1819. btn.disabled = false;
  1820. btn.classList.remove('styles_disabled__W_k45');
  1821. defaultPanoIdChange = false;
  1822. let position = new kakao.maps.LatLng(prevStep[0], prevStep[1]);
  1823. KakaoPlayer.setPanoId(prevStep[2], position);
  1824. document.getElementById("switch").lat = prevStep[0];
  1825. document.getElementById("switch").lng = prevStep[1];
  1826. document.getElementById("switch").useGoogle = false;
  1827. document.getElementById("Main Button").google = false;
  1828. // myLog("Undo 1 step");
  1829. // myLog(locHistory);
  1830. }
  1831. else if (nextPlayer === "Baidu") {
  1832. // myLog(prevStep[1]);
  1833. let position = new BMap.Point(prevStep[1], prevStep[0]);
  1834. let pov = { heading: prevStep[2], pitch: prevStep[3] };
  1835. BaiduPlayer.setPosition(position);
  1836. BaiduPlayer.setPov(pov);
  1837. document.getElementById("switch").lat = prevStep[1];
  1838. document.getElementById("switch").lng = prevStep[0];
  1839. }
  1840. else if (nextPlayer === "Mapillary" ) {
  1841. // myLog(prevStep[1]);
  1842.  
  1843. MapillaryPlayer.moveTo(prevStep[2]).then(
  1844. image => {
  1845. //myLog(image);
  1846. document.getElementById("switch").lat = prevStep[1];
  1847. document.getElementById("switch").lng = prevStep[0];
  1848. },
  1849. error => { myLog(error); });
  1850. }
  1851.  
  1852. }
  1853.  
  1854. function BYKTeleport()
  1855. {
  1856. let teleportButtonBYK = document.getElementById("Main Button");
  1857. if (teleportButtonBYK)
  1858. {
  1859. teleportButtonBYK.addEventListener("click", () => {
  1860. if (!teleportButtonBYK.google)
  1861. {
  1862. // myLog("non-Google Teleport");
  1863. let prevStep = null;
  1864. if (locHistory.length === 1) {
  1865. prevStep = locHistory[0];
  1866. }
  1867. else {
  1868. prevStep = locHistory[locHistory.length - 1];
  1869. }
  1870. // myLog(prevStep);
  1871. let options = {};
  1872. let place, position, pID;
  1873. if (nextPlayer === "Yandex") {
  1874. place = FindPointAtDistanceFrom(prevStep[0], prevStep[1], DegreesToRadians(prevStep[2]), teleportButtonBYK.distance * 0.001);
  1875. YandexPlayer.setDirection([prevStep[2], prevStep[3]]);
  1876. YandexPlayer.moveTo([place.lat, place.lng], options);
  1877. YandexPlayer.setSpan([10, 67]);
  1878. // locHistory.push([place.lat, place.lng, prevStep[2], prevStep[3]]);
  1879. }
  1880. else if (nextPlayer === "Kakao") {
  1881. // place = FindPointAtDistanceFrom(prevStep[0], prevStep[1], DegreesToRadians(prevStep[3]), teleportButtonBYK.distance * 0.001);
  1882. // position = new kakao.maps.LatLng(place.lat, place.lng);
  1883. // pID = KakaoPlayer.getViewpointWithPanoId();
  1884. // KakaoPlayer.setPanoId(pID.panoId, position);
  1885. // locHistory.push([place.lat, place.lng, pID.panoId, prevStep[3]]);
  1886. var roadviewClient = new kakao.maps.RoadviewClient();
  1887. place = FindPointAtDistanceFrom(prevStep[0], prevStep[1], DegreesToRadians(prevStep[3]), teleportButtonBYK.distance * 0.001);
  1888. position = new kakao.maps.LatLng(place.lat, place.lng);
  1889. roadviewClient.getNearestPanoId(position, 500, function (panoId) {
  1890. KakaoPlayer.setPanoId(panoId, position);
  1891. // myLog("Teleport 1 step");
  1892. // myLog(locHistory);
  1893. // locHistory.push([place.lat, place.lng, panoId, prevStep[3]]);
  1894. });
  1895. }
  1896. else if (nextPlayer === "Baidu") {
  1897. place = FindPointAtDistanceFrom(prevStep[0], prevStep[1], DegreesToRadians(prevStep[2]), teleportButtonBYK.distance * 0.001);
  1898. position = new BMap.Point(place.lng, place.lat);
  1899. let pov = { heading: prevStep[2], pitch: prevStep[3] };
  1900. BaiduPlayer.setPosition(position);
  1901. BaiduPlayer.setPov(pov);
  1902. // locHistory.push([place.lat, place.lng, prevStep[2], prevStep[3]]);
  1903. }
  1904. else if (nextPlayer === "Mapillary" || nextPlayer === "Google") {
  1905. place = FindPointAtDistanceFrom(prevStep[0], prevStep[1], DegreesToRadians(prevStep[2]), teleportButtonBYK.distance * 0.001);
  1906. handleMapillary(place, {meters: 500, limit: 500});
  1907. // locHistory.push([place.lat, place.lng, prevStep[2], prevStep[3]]);
  1908. }
  1909. document.getElementById("switch").lat = place.lat;
  1910. document.getElementById("switch").lng = place.lng;
  1911. if (teleportButtonBYK.distance > 150)
  1912. {
  1913. teleportButtonBYK.distance = 100;
  1914. teleportButtonBYK.innerHTML = "Teleport " + teleportButtonBYK.distance + " m";
  1915. }
  1916. }
  1917. });
  1918. }
  1919. else
  1920. {
  1921. }
  1922. }
  1923.  
  1924. function SyncListener()
  1925. {
  1926. let googleKakaoButton = document.getElementById("switch");
  1927. googleKakaoButton.addEventListener("click", () => {
  1928. if (googleKakaoButton.useGoogle == false) {
  1929. // googleKakaoButton.useGoogle = true;
  1930. if (googleKakaoButton.nextPlayer === "Yandex") {
  1931. let options = {};
  1932. YandexPlayer.moveTo([googleKakaoButton.lat, googleKakaoButton.lng], options);
  1933. YandexPlayer.setDirection([document.getElementById("switch").heading, 0]);
  1934.  
  1935. // document.getElementById("switch").nextPlayer = "Yandex";
  1936. }
  1937. if (googleKakaoButton.nextPlayer === "Baidu") {
  1938. let CANVAS = document.getElementById("PanoramaMap");
  1939. if (!googleKakaoButton.small_canvas)
  1940. {
  1941. CANVAS.style.width = window.innerHeight + "px";
  1942. if (isBattleRoyale && !isDuel)
  1943. {
  1944. CANVAS.style.left = (window.innerWidth - window.innerHeight)*0.78 / 2 + "px";
  1945. }
  1946. else
  1947. {
  1948. CANVAS.style.left = (window.innerWidth - window.innerHeight) / 2 + "px";
  1949. }
  1950. googleKakaoButton.small_canvas = true;
  1951. document.getElementById("switch").innerHTML = "Widescreen";
  1952. }
  1953. else
  1954. {
  1955. CANVAS.style.width = window.innerWidth + "px";
  1956. CANVAS.style.left = "0px";
  1957. googleKakaoButton.small_canvas = false;
  1958. document.getElementById("switch").innerHTML = "More Viewing Area";
  1959. }
  1960. }
  1961. else if (googleKakaoButton.nextPlayer === "Kakao") {
  1962. let roadviewClient = new kakao.maps.RoadviewClient();
  1963. // myLog(googleKakaoButton.lat);
  1964. let position = new kakao.maps.LatLng(googleKakaoButton.lat, googleKakaoButton.lng);
  1965. roadviewClient.getNearestPanoId(position, 500, function (panoId) {
  1966. KakaoPlayer.setPanoId(panoId, position);
  1967. });
  1968. KakaoPlayer.setViewpoint({
  1969. pan: document.getElementById("switch").heading,
  1970. tilt: 0,
  1971. zoom: 0
  1972. });
  1973. // document.getElementById("switch").nextPlayer = "Kakao";
  1974. }
  1975. else if (googleKakaoButton.nextPlayer === "Mapillary" || googleKakaoButton.nextPlayer === "Google") {
  1976. // document.getElementById("switch").nextPlayer = "Kakao";
  1977. handleMapillary({lat: googleKakaoButton.lat, lng: googleKakaoButton.lng}, {meters: 100, limit: 100});
  1978. }
  1979. }
  1980. });
  1981.  
  1982. }
  1983.  
  1984. /**
  1985. * Gets the seed data for the current game
  1986. *
  1987. * @returns Promise with seed data as object
  1988. */
  1989. function getSeed() {
  1990. // myLog("getSeed called");
  1991. return new Promise((resolve, reject) => {
  1992. let token = getToken();
  1993. let URL;
  1994. let cred = ""
  1995.  
  1996. const PATHNAME = window.location.pathname;
  1997.  
  1998. if (PATHNAME.startsWith("/game/")) {
  1999. URL = `https://www.geoguessr.com/api/v3/games/${token}`;
  2000. }
  2001. else if (PATHNAME.startsWith("/challenge/")) {
  2002. URL = `https://www.geoguessr.com/api/v3/challenges/${token}/game`;
  2003. }
  2004. else if (PATHNAME.startsWith("/battle-royale/")) {
  2005. URL = `https://game-server.geoguessr.com/api/battle-royale/${token}`;
  2006. }
  2007. else if (PATHNAME.startsWith("/duels/") || PATHNAME.startsWith("/team-duels/")) {
  2008. URL = `https://game-server.geoguessr.com/api/duels/${token}`;
  2009. }
  2010. if (isBattleRoyale) {
  2011. fetch(URL, {
  2012. // Include credentials to GET from the endpoint
  2013. credentials: 'include'
  2014. })
  2015. .then((response) => response.json())
  2016. .then((data) => {
  2017. resolve(data);
  2018. })
  2019. .catch((error) => {
  2020. reject(error);
  2021. });
  2022. }
  2023. else {
  2024. fetch(URL)
  2025. .then((response) => response.json())
  2026. .then((data) => {
  2027. resolve(data);
  2028. })
  2029. .catch((error) => {
  2030. reject(error);
  2031. });
  2032. }
  2033. });
  2034. }
  2035.  
  2036. /**
  2037. * Gets the token from the current URL
  2038. *
  2039. * @returns token
  2040. */
  2041. function getToken() {
  2042. const PATHNAME = window.location.pathname;
  2043. if (PATHNAME.startsWith("/game/")) {
  2044. return PATHNAME.replace("/game/", "");
  2045. }
  2046. else if (PATHNAME.startsWith("/challenge/")) {
  2047. return PATHNAME.replace("/challenge/", "");
  2048. }
  2049. else if (PATHNAME.startsWith("/battle-royale/")) {
  2050. return PATHNAME.replace("/battle-royale/", "");
  2051. }
  2052. else if (PATHNAME.startsWith("/duels/")) {
  2053. return PATHNAME.replace("/duels/", "");
  2054. }
  2055. else if (PATHNAME.startsWith("/team-duels/")) {
  2056. return PATHNAME.replace("/team-duels/", "");
  2057. }
  2058. }
  2059.  
  2060. /**
  2061. * Gets the round number from the ongoing game from the page itself
  2062. *
  2063. * @returns Round number
  2064. */
  2065. function getRoundFromPage() {
  2066. const roundData = document.querySelector("div[data-qa='round-number']");
  2067. if (roundData) {
  2068. let roundElement = roundData.querySelector("div:last-child");
  2069. if (roundElement) {
  2070. let round = parseInt(roundElement.innerText.charAt(0));
  2071. if (!isNaN(round) && round >= 1 && round <= 5) {
  2072. return round;
  2073. }
  2074. }
  2075. }
  2076. else {
  2077. return ROUND;
  2078. }
  2079. }
  2080.  
  2081.  
  2082. /**
  2083. * Injects Yandex Script
  2084. */
  2085. function injectYandexScript() {
  2086. return new Promise((resolve, reject) => {
  2087. if (!YANDEX_INJECTED) {
  2088. if (YANDEX_API_KEY === "") {
  2089. myLog("No Yandex Key")
  2090. reject();
  2091. }
  2092. else {
  2093. const SCRIPT = document.createElement("script");
  2094. SCRIPT.type = "text/javascript";
  2095. SCRIPT.async = true;
  2096. SCRIPT.onload = () => {
  2097. ymaps.ready(() => {
  2098. YANDEX_INJECTED = true;
  2099. myHighlight("Yandex API Loaded");
  2100. resolve();
  2101. });
  2102. }
  2103. SCRIPT.src = `https://api-maps.yandex.ru/2.1/?lang=en_US&apikey=${YANDEX_API_KEY}`;
  2104. document.body.appendChild(SCRIPT);
  2105. }
  2106. }
  2107. else {
  2108. resolve();
  2109. }
  2110. });
  2111. }
  2112.  
  2113. /**
  2114. * Injects Yandex Player and calls handleReturnToStart
  2115. */
  2116. function injectYandexPlayer() {
  2117. let lat = 41.321861;
  2118. let lng = 69.212920;
  2119.  
  2120. let options = {
  2121. "direction": [0, 16],
  2122. "span": [10, 67],
  2123. "controls": ["zoomControl"]
  2124. };
  2125. ymaps.panorama.createPlayer("player", [lat, lng], options)
  2126. .done((player) => {
  2127. YandexPlayer = player;
  2128. YandexPlayer.events.add("directionchange", (e) => {
  2129. updateCompass();
  2130. let pov = YandexPlayer.getDirection();
  2131. if (locHistory.length > 0 && nextPlayer == "Yandex") {
  2132. document.getElementById("switch").heading = pov[0];
  2133. locHistory[locHistory.length - 1][2] = pov[0];
  2134. locHistory[locHistory.length - 1][3] = pov[1];
  2135. }
  2136. });
  2137. YandexPlayer.events.add("panoramachange", (e) => {
  2138. if (defaultPanoIdChange) {
  2139. let num = YandexPlayer.getPanorama().getPosition();
  2140. let pov = YandexPlayer.getDirection();
  2141. // myLog(num);
  2142. // myLog(pov);
  2143. if (nextPlayer == "Yandex")
  2144. {
  2145. locHistory.push([num[0], num[1], pov[0], pov[1]]);
  2146. document.getElementById("switch").lat = num[0];
  2147. document.getElementById("switch").lng = num[1];
  2148. }
  2149. let btn = document.querySelector("button[data-qa='undo-move']");
  2150. if (locHistory.length > 1) {
  2151. btn.disabled = false;
  2152. btn.classList.remove('styles_disabled__W_k45');
  2153. }
  2154. // myLog(locHistory);
  2155. }
  2156. defaultPanoIdChange = true;
  2157.  
  2158. });
  2159. myLog("Yandex Player injected");
  2160. });
  2161.  
  2162. }
  2163.  
  2164.  
  2165. /**
  2166. * Injects Baidu script
  2167. */
  2168.  
  2169. function injectBaiduScript() {
  2170. return new Promise((resolve, reject) => {
  2171. if (!BAIDU_INJECTED) {
  2172. if (BAIDU_API_KEY === "") {
  2173. let canvas = document.getElementById("player");
  2174. myLog("No Baidu Key")
  2175. }
  2176. else {
  2177. const SCRIPT = document.createElement("script");
  2178. SCRIPT.type = "text/javascript";
  2179. SCRIPT.async = true;
  2180. SCRIPT.src = `https://api.map.baidu.com/api?v=3.0&ak=${BAIDU_API_KEY}&callback=init`;
  2181. document.body.appendChild(SCRIPT);
  2182.  
  2183. let canvas = document.createElement("bmap");
  2184. if (isBattleRoyale) {
  2185. if (isDuel)
  2186. {
  2187. canvas.innerHTML = `
  2188. <div id="PanoramaMap" class="inactive" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  2189. `;
  2190. }
  2191. else
  2192. {
  2193. canvas.innerHTML = `
  2194. <div id="PanoramaMap" class="br-game-layout__panorama" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  2195. `;
  2196. }
  2197. }
  2198. else {
  2199. canvas.innerHTML = `
  2200. <div id="PanoramaMap" class="game-layout__panorama" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  2201. `;
  2202. }
  2203.  
  2204. var div = document.getElementById("player");
  2205. div.appendChild(canvas);
  2206.  
  2207. SCRIPT.addEventListener('load', () => {
  2208. myHighlight("Baidu API Loaded");
  2209. // resolve(BMap);
  2210. let timeout = 0;
  2211. let interval = setInterval(() => {
  2212. if (timeout >= 40) {
  2213. reject();
  2214. clearInterval(interval);
  2215. }
  2216. if (typeof BMap.Panorama !== typeof undefined) {
  2217. BAIDU_INJECTED = true;
  2218. resolve(BMap);
  2219. clearInterval(interval);
  2220. }
  2221. timeout += 1;
  2222. }, 500);
  2223. })
  2224.  
  2225.  
  2226. }
  2227. }
  2228. else {
  2229. resolve();
  2230. }
  2231. });
  2232. }
  2233.  
  2234. /**
  2235. * Injects Baidu Player and calls handleReturnToStart
  2236. */
  2237. function injectBaiduPlayer() {
  2238. injectBaiduScript().then(BMap => {
  2239. BaiduPlayer = new BMap.Panorama('PanoramaMap');
  2240. BaiduPlayer.setPov({ heading: -40, pitch: 6 });
  2241. BaiduPlayer.setPosition(new BMap.Point(0, 0));
  2242. if (!eventListenerAttached && !povListenerAttached) {
  2243. myLog("position_listener attached");
  2244. BaiduPlayer.addEventListener('position_changed', function (e) {
  2245. eventListenerAttached = true;
  2246. myLog('position_changed')
  2247. let num = BaiduPlayer.getPosition();
  2248. let pov = BaiduPlayer.getPov();
  2249. if (nextPlayer == "Baidu" && num.lat != 0)
  2250. {
  2251. locHistory.push([num.lat, num.lng, pov.heading, pov.pitch]);
  2252. }
  2253. if (!isBattleRoyale || isDuel)
  2254. {
  2255. let btn = document.querySelector("button[data-qa='undo-move']");
  2256. if (locHistory.length > 1) {
  2257. btn.disabled = false;
  2258. btn.classList.remove('styles_disabled__W_k45');
  2259. }
  2260. }
  2261. // myLog(locHistory);
  2262. })
  2263.  
  2264. BaiduPlayer.addEventListener('pov_changed', function (e) {
  2265. // myLog("pov_listener attached");
  2266. povListenerAttached = true;
  2267. // myLog('pov_changed')
  2268. let pov = BaiduPlayer.getPov();
  2269. if (locHistory.length > 0 && nextPlayer == "Baidu") {
  2270. locHistory[locHistory.length - 1][2] = pov.heading;
  2271. locHistory[locHistory.length - 1][3] = pov.pitch;
  2272. }
  2273. // myLog(locHistory);
  2274. })
  2275. }
  2276. });
  2277. }
  2278.  
  2279. /**
  2280. * Injects Kakao script
  2281. */
  2282.  
  2283. function injectKakaoScript() {
  2284. return new Promise((resolve, reject) => {
  2285. if (!KAKAO_INJECTED) {
  2286. if (KAKAO_API_KEY === "") {
  2287. myLog("No Kakao Key")
  2288. }
  2289. else {
  2290.  
  2291. let canvas = document.createElement("kmap");
  2292. if (isBattleRoyale) {
  2293. if (isDuel)
  2294. {
  2295. canvas.innerHTML = `
  2296. <div id="roadview" class="inactive" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  2297. `;
  2298. }
  2299. else
  2300. {
  2301. canvas.innerHTML = `
  2302. <div id="roadview" class="br-game-layout__panorama" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  2303. `;
  2304. }
  2305. }
  2306. else {
  2307. canvas.innerHTML = `
  2308. <div id="roadview" class="game-layout__panorama" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  2309. `;
  2310. }
  2311.  
  2312. var div = document.getElementById("player");
  2313. div.appendChild(canvas);
  2314.  
  2315. const SCRIPT = document.createElement("script");
  2316. SCRIPT.async = true;
  2317. // SCRIPT.type = "text/javascript";
  2318. SCRIPT.src = `//dapi.kakao.com/v2/maps/sdk.js?appkey=${KAKAO_API_KEY}&autoload=false`;
  2319. document.body.appendChild(SCRIPT);
  2320. SCRIPT.onload = () => {
  2321. kakao.maps.load(function () {
  2322. var position = new kakao.maps.LatLng(33.450701, 126.560667);
  2323. let roadviewContainer = document.getElementById('roadview');
  2324. KakaoPlayer = new kakao.maps.Roadview(roadviewContainer);
  2325. var panoId = 1023434522;
  2326. KakaoPlayer.setPanoId(panoId, position);
  2327. KAKAO_INJECTED = true;
  2328. // Remove the compass from Kakao
  2329. kakao.maps.event.addListener(KakaoPlayer, 'init', () => {
  2330. const compassContainer = roadviewContainer.querySelector('div[id*="_box_util_"]');
  2331. if (compassContainer) compassContainer.style.display = 'none';
  2332. });
  2333. kakao.maps.event.addListener(KakaoPlayer, 'panoid_changed', function() {
  2334. if (defaultPanoIdChange) {
  2335. let latlng = KakaoPlayer.getPosition();
  2336. let lat = latlng.getLat();
  2337. let lng = latlng.getLng();
  2338. let pID = KakaoPlayer.getViewpointWithPanoId();
  2339. if (nextPlayer == "Kakao" && lat != 33.45047613915499)
  2340. {
  2341. // myLog("push");
  2342. locHistory.push([lat, lng, pID.panoId, pID.pan]);
  2343. document.getElementById("switch").lat = lat;
  2344. document.getElementById("switch").lng = lng;
  2345. document.getElementById("switch").heading = pID.pan;
  2346. }
  2347. let btn = document.querySelector("button[data-qa='undo-move']");
  2348. if (locHistory.length > 1 && (btn != null)) {
  2349. btn.disabled = false;
  2350. btn.classList.remove('styles_disabled__W_k45');
  2351. }
  2352. // myLog(locHistory);
  2353. }
  2354. defaultPanoIdChange = true;
  2355. });
  2356. kakao.maps.event.addListener(KakaoPlayer, 'viewpoint_changed', function() {
  2357. // myLog("pov_listener attached");
  2358. let pID = KakaoPlayer.getViewpointWithPanoId();
  2359. if (locHistory.length > 0 && nextPlayer == "Kakao") {
  2360. document.getElementById("switch").heading = pID.pan;
  2361. locHistory[locHistory.length - 1][3] = pID.pan;
  2362. }
  2363. if (GooglePlayer) {
  2364. const { heading, pitch } = GooglePlayer.getPov()
  2365. if (!almostEqual(pID.pan, heading) || !almostEqual(pID.tilt, pitch)) {
  2366. // Updating the google street view POV will update the compass
  2367. GooglePlayer.setPov({ heading: pID.pan, pitch: pID.tilt })
  2368. }
  2369. }
  2370. // myLog(locHistory);
  2371. })
  2372. myHighlight("Kakao API Loaded");
  2373. resolve();
  2374. });
  2375. };
  2376.  
  2377. }
  2378. }
  2379. else {
  2380. resolve();
  2381. }
  2382. });
  2383. }
  2384.  
  2385.  
  2386.  
  2387. function injectMapillaryPlayer() {
  2388. return new Promise((resolve, reject) => {
  2389. if (!MAPILLARY_INJECTED) {
  2390. if (MAPILLARY_API_KEY === "") {
  2391. let canvas = document.getElementById("player");
  2392. myLog("No Mapillary Key")
  2393. }
  2394. else {
  2395. const SCRIPT = document.createElement("script");
  2396. SCRIPT.type = "text/javascript";
  2397. SCRIPT.async = true;
  2398. SCRIPT.src = `https://unpkg.com/mapillary-js@4.0.0/dist/mapillary.js`;
  2399. document.body.appendChild(SCRIPT);
  2400. document.querySelector('head').innerHTML += '<link href="https://unpkg.com/mapillary-js@4.0.0/dist/mapillary.css" rel="stylesheet"/>';
  2401. let canvas = document.createElement("mmap");
  2402. if (isBattleRoyale) {
  2403. if (isDuel)
  2404. {
  2405.  
  2406. canvas.innerHTML = `<div id="mly" class="inactive" style="zIndex: 99999, position: 'absolute', top: 0, left: 0, width: '100%', height: '100%'"></div>`;
  2407. }
  2408. else
  2409. {
  2410. canvas.innerHTML = `<div id="mly" class="br-game-layout__panorama" style="zIndex: 99999, position: 'absolute', top: 0, left: 0, width: '100%', height: '100%'"></div>`;
  2411. }
  2412. }
  2413. else {
  2414. canvas.innerHTML = `<div id="mly" class="game-layout__panorama" style="zIndex: 99999, position: 'absolute', top: 0, left: 0, width: '100%', height: '100%'"></div>`;
  2415. }
  2416.  
  2417. var div = document.getElementById("player");
  2418. div.appendChild(canvas);
  2419.  
  2420. SCRIPT.addEventListener('load', () => {
  2421. myHighlight("Mapillary API Loaded");
  2422. // resolve(BMap);
  2423. var {Viewer} = mapillary;
  2424.  
  2425. MapillaryPlayer = new Viewer({
  2426. accessToken: MAPILLARY_API_KEY,
  2427. container: 'mly', // the ID of our container defined in the HTML body
  2428. });
  2429.  
  2430. MapillaryPlayer.on('image', async (event) => {
  2431. // cnt = cnt + 1;
  2432. // myLog(cnt);
  2433. let image = event.image;
  2434. let pos = image.originalLngLat;
  2435. let cond = true;
  2436. for (const element of locHistory) {
  2437. if (element[2] == image.id)
  2438. {
  2439. cond = false;
  2440. }
  2441. }
  2442. if (cond)
  2443. {
  2444. document.getElementById("switch").lat = pos.lat;
  2445. document.getElementById("switch").lng = pos.lng;
  2446. document.getElementById("switch").heading = image.compassAngle;
  2447. // myLog(pos);
  2448. locHistory.push([pos.lat, pos.lng, image.id, image.compassAngle]);
  2449. }
  2450. let btn = document.querySelector("button[data-qa='undo-move']");
  2451. if (btn !== null && locHistory.length > 1)
  2452. {
  2453. btn.disabled = false;
  2454. btn.classList.remove('styles_disabled__W_k45');
  2455. }
  2456. });
  2457.  
  2458. MAPILLARY_INJECTED = true;
  2459. resolve();
  2460. })
  2461.  
  2462.  
  2463. }
  2464. }
  2465. else {
  2466. resolve();
  2467. }
  2468. });
  2469. }
  2470.  
  2471.  
  2472. function handleMapillary(latlng, options)
  2473. {
  2474. myLog("handleMapillary")
  2475. handleMapillaryHelper(latlng, options).then((data) => {
  2476. //myLog(data.data)
  2477. let idToSet = 0;
  2478. let curDist = 100000000;
  2479. for (const element of data.data) {
  2480. // myLog(element)
  2481. if (element.hasOwnProperty("computed_geometry"))
  2482. {
  2483. try {
  2484. let rCord = element.computed_geometry["coordinates"];
  2485. let dist = distance(latlng.lat,latlng.lng,rCord[1],rCord[0])
  2486. if (dist < curDist)
  2487. {
  2488. idToSet = element.id;
  2489. curDist = dist
  2490. }
  2491. } catch (e) {
  2492. myLog("Error")
  2493. }
  2494. }
  2495. }
  2496. if (idToSet !== 0)
  2497. {
  2498. MapillaryPlayer.moveTo(idToSet).then(
  2499. image => { //myLog(image);
  2500. },
  2501. error => { myLog(error); });
  2502. }}).catch((error) => {
  2503. myLog(error);
  2504. });
  2505. }
  2506.  
  2507. function handleMapillaryHelper(latlng, options)
  2508. {
  2509. return new Promise((resolve, reject) => {
  2510. // myLog("1")
  2511. let bbox = getBBox(latlng, options.meters);
  2512. let URL = "https://graph.mapillary.com/images?access_token={0}&fields=id,computed_geometry&bbox={1}&limit={2}".replace('{0}', MAPILLARY_API_KEY).replace('{1}', bbox).replace('{2}', options.limit)
  2513. // myLog(URL)
  2514. fetch(URL)
  2515. .then((response) => {resolve(response.json())})
  2516. .catch((error) => {myLog(error);});
  2517. });
  2518. }
  2519.  
  2520. function moveFrom(coords, angle, distance){
  2521. const R_EARTH = 6378.137;
  2522. const M = (1 / ((2 * Math.PI / 360) * R_EARTH)) / 1000;
  2523. let radianAngle = -angle * Math.PI / 180;
  2524. let x = 0 + (distance * Math.cos(radianAngle));
  2525. let y = 0 + (distance * Math.sin(radianAngle));
  2526.  
  2527. let newLat = coords.lat + (y * M);
  2528. let newLng = coords.lng + (x * M) / Math.cos(coords.lat * (Math.PI / 180));
  2529. return { lat: newLat, lng: newLng };
  2530. }
  2531.  
  2532. function getBBox(coordinates, meters){
  2533. let SW = moveFrom(coordinates, 135, meters);
  2534. let NE = moveFrom(coordinates, 315, meters);
  2535. return `${SW.lng},${SW.lat},${NE.lng},${NE.lat}`;
  2536. }
  2537.  
  2538.  
  2539. function distance(lat1, lon1, lat2, lon2) {
  2540. var p = 0.017453292519943295; // Math.PI / 180
  2541. var c = Math.cos;
  2542. var a = 0.5 - c((lat2 - lat1) * p)/2 +
  2543. c(lat1 * p) * c(lat2 * p) *
  2544. (1 - c((lon2 - lon1) * p))/2;
  2545.  
  2546. return 1000 * 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
  2547. }