Last active
June 21, 2025 06:12
-
-
Save yousuf-hossain-shanto/6e85a9828188de341f4132dc4079615e to your computer and use it in GitHub Desktop.
Get position (latitude, longitude) from google map embed code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { load } from "cheerio" | |
| export const getPositionFromGoogleMapEmbedCode = (embedCode: string) => { | |
| const $ = load(embedCode) | |
| const iframe = $("iframe"); | |
| let position = { | |
| latitude: undefined, | |
| longitude: undefined, | |
| } as { | |
| latitude?: number; | |
| longitude?: number; | |
| }; | |
| if (iframe) { | |
| const embedUrlString = iframe.attr("src"); | |
| if (embedUrlString) { | |
| try { | |
| const embedUrl = new URL(embedUrlString); | |
| const pbQueryString = embedUrl.searchParams.get("pb"); | |
| if (pbQueryString) { | |
| const filtered = pbQueryString | |
| .split("!") | |
| .filter((part) => part.indexOf("d") === 1); | |
| position = filtered.reduce( | |
| (acc, cur) => { | |
| if (cur.charAt(0) === "2") { | |
| acc.longitude = Number(cur.substring(2)); | |
| } else if (cur.charAt(0) === "3") { | |
| acc.latitude = Number(cur.substring(2)); | |
| } | |
| return acc; | |
| }, | |
| position | |
| ); | |
| } | |
| } catch (e) { | |
| } | |
| } | |
| } | |
| return position; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment