An open source location picker plugin using Google Maps v3, written in modern TypeScript, shipped as both ESM and CJS with full type definitions.
v2 is a modernization release. See the Migration from v1 section below.
npm install location-pickerYou also need the Google Maps JavaScript API loaded on your page (either via the classic script tag or the official @googlemaps/js-api-loader).
import { LocationPicker } from 'location-picker';
import 'location-picker/style.css';
const lp = new LocationPicker(
'map',
{ setCurrentPosition: true, onLocationChange: (p) => console.log(p) },
{ zoom: 15 },
);const { LocationPicker } = require('location-picker');
require('location-picker/style.css');
const lp = new LocationPicker('map');If you load the Google Maps marker library, set useAdvancedMarker: true and the picker will use google.maps.marker.AdvancedMarkerElement instead of the CSS-pin overlay:
const lp = new LocationPicker('map', { useAdvancedMarker: true }, { mapId: 'YOUR_MAP_ID' });element: string | HTMLElement- element id or DOM node.options: LocationPickerOptions:setCurrentPosition?: boolean- defaulttrue. Skipped iflat/lngprovided.lat?: number,lng?: number- initial center.useAdvancedMarker?: boolean- defaultfalse.onLocationChange?: (pos: LatLng) => void- called on eachidleevent.
mapOptions: google.maps.MapOptions- forwarded tonew google.maps.Map.
getMarkerPosition(): { lat: number; lng: number }setLocation(lat: number, lng: number): voidsetCurrentPosition(): Promise<{ lat: number; lng: number }>- breaking in v2, now returns a Promise.destroy(): void- removes listeners and the marker DOM.
import type { LatLng, LocationPickerOptions } from 'location-picker';| v1 | v2 |
|---|---|
import LocationPicker from 'location-picker' |
Still works. Also: import { LocationPicker } from 'location-picker'. |
setCurrentPosition() returned void |
Returns Promise<LatLng>. Rejects on failure instead of console.log. |
| UMD / minified UMD bundle | Removed. Use ESM (dist/index.mjs) or CJS (dist/index.cjs). |
@types/googlemaps |
Replaced by @types/google.maps. |
| CSS was bundled automatically | Now opt-in: import 'location-picker/style.css'. |
No destroy() |
destroy() available for cleanup (SPAs, HMR). |
| No idle callback | onLocationChange option. |
| No AdvancedMarker | useAdvancedMarker: true opts into AdvancedMarkerElement. |
Node >=18 is required to build/develop the library. Consumers are unaffected.
npm install
npm run lint
npm run typecheck
npm run test
npm run buildMIT