1.0.3 • Published yesterday
get-gplace-id
Licence
ISC
Version
1.0.3
Deps
0
Size
32 kB
Vulns
0
Weekly
0
get-gplace-id
A robust TypeScript library to extract the Google Place ID from various types of Google Maps URLs.
This package handles standard URLs, short URLs, mobile share links, and complex search intent URLs by using a combination of direct extraction, URL expansion, and the Google Places API.
Installation
npm install get-gplace-id
bun install get-gplace-id
pnpm install get-gplace-id
Usage
You must provide a valid Google Maps API Key with access to the Places API (specifically Text Search and Find Place from Text).
import { getGPlaceId } from "get-gplace-id";
async function main() {
const apiKey = "YOUR_GOOGLE_MAPS_API_KEY";
const url = "https://goo.gl/maps/YOUR_SHORT_URL";
const placeId = await getGPlaceId(url, { apiKey });
if (placeId) {
console.log("Found Place ID:", placeId);
} else {
console.log("Could not extract Place ID.");
}
}
main();
Supported URL Formats
The library supports a wide range of URL types:
- Direct Place ID: URLs that explicitly contain
place_id=...parameters. - Short URLs:
maps.app.goo.gl,goo.gl/maps, andshare.googlelinks (handles redirects automatically). - Regular Maps URLs:
- Extracts coordinates (
!3d...!4d...) and uses the Text Search API with name matching and distance scoring. - Uses Find Place from Text API if no coordinates are found.
- Extracts coordinates (
- Search Intent URLs: Handles complex URLs containing
pvqorlqiparameters (often from mobile apps) by decoding the query intent and scraping coordinates from the page signature to perform a precise search. - Search Redirects: Handles URLs that redirect to
google.com/search?q=...(common withshare.googlelinks).
API Reference
getGPlaceId(url: string, options: GPlaceIdOptions): Promise<string | null>
url: The Google Maps URL string to parse.options: Configuration object.apiKey: Required. Your Google Cloud API Key.
Returns a Promise that resolves to the Place ID string if found, or null if not.
How it works
- Direct Check: Checks if the URL already contains a Place ID.
- Expansion: If it's a short URL (
maps.app.goo.gl,share.google), it follows HTTP redirects to get the final canonical URL. - Search Intent: If it's a "search intent" URL (protobuf parameters), it decodes the query and location to perform a targeted API search.
- Regular Resolution: Parses the URL path (e.g., place name) and coordinates. It then queries the Google Places API and scores results based on:
- Name Match: Exact vs. Partial match.
- Distance: Proximity to the coordinates found in the URL.
License
ISC