Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save yousuf-hossain-shanto/6e85a9828188de341f4132dc4079615e to your computer and use it in GitHub Desktop.

Select an option

Save yousuf-hossain-shanto/6e85a9828188de341f4132dc4079615e to your computer and use it in GitHub Desktop.
Get position (latitude, longitude) from google map embed code.
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