npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gmaps-address-locator

v1.0.39

Published

A small utility that allows easier integration with google maps to be mainly used to enable users to select their address on the map

Downloads

41

Readme

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

Gmap-address-locator

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';

// User defined configs
const config = {
	// When locale is set, it does the following:
	// Sets the autocomplete preferred region
	// Enables geofencing
	// On map load, centers the map on this locale
	locale: null,
	// A helper setting for usage on mobile devices, currently, it removes
	// zoom controls, WIP: pan map around a fixed marker for better UX
	mobileView: false,
	// Load the map on a specific position, should be an object in the following format:
	// { lng: LONGITUDE, lat: LATITUDE }
	initialPosition: null,
	// Enables autocomplete, enter the autocomplete html input field id
	// Hint: preferably, add a .hidden class to the input field to prevent flicker
	// effect from page load to map load
	autocompleteFieldId: null,
	// Adds locate me button to map, enter button id
	locateMeBtnId: null,
	// Adds a main CTA button at the bottom center of the map, event listener can
	// be added through myMap.onConfirm button (described below)
	confirmBtn: null
	// Adds a secondary CTA button at the top center of the map (to the right of the autocomplete field if enabled)
	// event listener can be added through myMap.onSecondaryAction (described below)
	secondaryActionBtn: null,
}

// Google maps native settings, check google maps JS api documentation for more info
// Example:
const mapSettings = {
	zoom: 4,
	center: myLatLng,
	mapTypeId: 'satellite',
	etc..
}

// @param1: map id
// @param2: user defined configs
// @param3: google maps settings
const myMap = new gmapsAddressLocator('map', config, mapSettings);

// Call back function for when everything is initiated and ready to go
myMap.onReady(() => console.log('Ready to go!'));

// Call back function for the click event of the confirm button
// Passes the currently selected location result as parameter
myMap.onConfirm(res => console.log('Confirm button clicked: ', res));

// Call back function for the click event of the secondary button
myMap.onSecondaryAction(() => console.log('do something'));

// Call back function for any location selection/update event,
// event could be fired on a drag end event or an autocomplete option selection.
// Passes the currently selected location result as parameter
myMap.onLocationSelection(res => console.log(res));

// If initializing the map, the autocomplete field or the locate me button fails, 
// this hook will be called
myMap.onFail(error => console.log(error));

// Other helper functions:
// Shows map
myMap.showMap();

// Hides map
myMap.hideMap();

HTML

<!DOCTYPE html>
<html>
<head>
	<title>Gmaps address locator - example</title>
</head>
<body>
	<!-- Main map container, make sure this is 100% height -->
	<div id="map" class="map" style="height: 100%"></div>
	
	<!-- Autocomplete search input field -->
	<input id="gmap-autocomplete-input" class="pac-input controls hidden" type="text" placeholder="Search addresses" />
	
	<!-- Confirm button -->
	<button id="gmap-confirm-btn" class="hidden">Confirm location</button>
	
	<!-- Secondary action button -->
	<button id="gmap-add-manually-btn" class="hidden">Add manually</button>
	
	<!-- Locate me button -->
	<div id="gmap-locate-me-btn" class="hidden">Locate me</div>


	<!-- Make sure to update this script with your api key -->
	<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:

{
	// Native google maps fields
  "address_components",
  "formatted_address",
  "geometry"
  "place_id",
  "plus_code",
  "types",
  // Custom fields
  "lngLat",
  "formatted_address2",
  "city"
}