Skip to content

Instantly share code, notes, and snippets.

@albasyir
Created June 24, 2024 06:39
Show Gist options
  • Save albasyir/6f3c63a34f2de1c5495bb0b515f26211 to your computer and use it in GitHub Desktop.
Save albasyir/6f3c63a34f2de1c5495bb0b515f26211 to your computer and use it in GitHub Desktop.
Get longitude and latitude from google map link and get gmap link from latitude and longitude
const links = [
// random
"https://www.google.com/maps/@-6.1619199,106.8530092,15.76z?entry=ttu",
// unmatch url
"https://facebook.com/@-6.1619199/area",
// marchan clicked
"https://www.google.com/maps/place/TanSu+Kemayoran/@-6.1619199,106.8530092,15.76z/data=!4m6!3m5!1s0x2e69f5bb8fcb8b41:0xec7e3f7c984200a4!8m2!3d-6.1608762!4d106.8493247!16s%2Fg%2F11fxdvf1k9?entry=ttu",
// area clicked (this only for central of area not area it self)
"https://www.google.com/maps/search/Parkir/@-6.1582122,106.8416849,13.82z/data=!4m7!2m6!3m5!2sAula+Simfonia+Jakarta!3s0x2e69f594e0468145:0xa9393e8a78202634!4m2!1d106.8441586!2d-6.1533959?entry=ttu"
];
function extractLatLong(url: string): { latitude: number, longitude: number } | null {
const regex = /@(-?\d+\.\d+),(-?\d+\.\d+)/;
const match = url.match(regex);
if(!match) return null
const latitude = parseFloat(match[1]);
const longitude = parseFloat(match[2]);
return { latitude, longitude };
}
function getGoogleMapsUrl(lat: number, long: number, options?: {
zoom?: number
}) {
const props = {
zoom: options?.zoom || 14
}
return `https://maps.google.com/maps?q=${lat},${long}&hl=es&z=${props}&output=embed`
}
console.log(
links.map(link => {
const coordinate = extractLatLong(link);
if(!coordinate) return;
return getGoogleMapsUrl(coordinate.latitude, coordinate.longitude)
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment