Gmaps Address Locator
A small utility that allows easier integration with google maps to be mainly used to enable users to select their address on the map.
✨ Features
- Enables users to select location, location selections are represented with a marker and an information popup
- Autocomplete input search box
- User current location detection via navigator's geolocation api
- Options to add main and secondary call to action buttons with event listeners on the map
- Geofencing through locale (a check if selected point is within the selected locale region)
- API to retrieve currently selected locations and set callbacks on various events like dragend and confirm button click
Install
npm i gmaps-address-locator
Screenshot

Notes
- Make sure to load the google maps JS api in your app with the appropriate API Key, check google maps JS documentation for more information. Gmaps address locator will not work if the google maps JS api is not loaded
- To enable autocomplete feature, add the places library when you're loading the google maps JS api
- To setup language, add the language param when loading the google maps JS api
- While using the navigator's geolocation api, users are prompted for access to location, if allowed, we pass and initiate the map with that location, if blocked, we show the map centered based on locale settings
Usage
Javascript Setup
import gmapsAddressLocator from 'gmaps-address-locator';
const config = {
locale: null,
mobileView: false,
initialPosition: null,
autocompleteFieldId: null,
locateMeBtnId: null,
confirmBtn: null
secondaryActionBtn: null,
}
const mapSettings = {
zoom: 4,
center: myLatLng,
mapTypeId: 'satellite',
etc..
}
const myMap = new gmapsAddressLocator('map', config, mapSettings);
myMap.onConfirm(res => console.log('Confirm button clicked: ', res));
myMap.onSecondaryAction(() => console.log('do something'));
myMap.onLocationSelection(res => console.log(res));
myMap.showMap();
myMap.hideMap();
HTML
<!DOCTYPE html>
<html>
<head>
<title>Gmaps address locator - example</title>
</head>
<body>
<div id="map" class="map" style="height: 100%"></div>
<input id="gmap-autocomplete-input" class="pac-input controls hidden" type="text" placeholder="Search addresses" />
<button id="gmap-confirm-btn" class="hidden">Confirm location</button>
<button id="gmap-add-manually-btn" class="hidden">Add manually</button>
<div id="gmap-locate-me-btn" class="hidden">Locate me</div>
<script src="https://maps.googleapis.com/maps/api/js?key=ENTER_YOUR_API_KEY_HERE&libraries=places"></script>
</body>
</html>
CSS
For CSS, feel free to style those buttons as you like!
Selected location object output
When retrieving the selected location object, we return the result of the google maps geolocation or autocomplete search query, plus a couple of custom fields with a bit more data, sample output:
{
"address_components",
"formatted_address",
"geometry"
"place_id",
"plus_code",
"types",
"lngLat",
"formatted_address2",
"city"
}