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

google-maps-current-location

v0.4.0

Published

Google maps current location position and marker

Downloads

1,167

Readme

Google Maps Current Location

Latest version npm

Click on your location and display the geographic location of a user or device on a Google map.

Usage

Install the library and add it to your code:

npm install google-maps-current-location
import addCurrentLocation from 'google-maps-current-location'

// ...

addCurrentLocation(map)

or add it directly to your .html file using unpkg:

<script src="https://unpkg.com/google-maps-current-location"></script>

<script>
  // ...
  
  addCurrentLocation(map)

</script>

Props

addCurrentLocation(map, options)

Required

map represents a google.maps.Map.
Check out how to configure it here.

Optional

options is an object with the following elements:

1. buttonStyle

Configures the css and positioning of the button displayed over the map.

Prop | Type | Description | Default ---- | ----| ----------- | ------- buttonPosition | google.maps.ControlPosition | Position of button in relation with map. Check here to find out more. | google.maps.ControlPosition.RIGHT_BOTTOM mainMargin| string | The button's margin | 10px backgroundColor|string| The button's background color. All CSS3 colors are supported|#fff symbolColor|string|The symbol's fill color. Only HEX color supported|#6F6F6F border|string|The button's border|0px borderRadius|string|The button's border radius|0px outline|string|The button's outline|0px height|string|The button's height (height===width)|40px boxShadow|string|The button's boxShadow|0 1px 4px rgba(0,0,0,0.3) cursor|string|Mouse cursor type to show on hover|pointer

2. markerStyle

Configures the css of the marker displayed over the map, on the current location coordinates. To learn more about google.maps.marker.AdvancedMarkerElement click here.

Prop | Type | Description | Default ---- | ----| ----------- | ------- clickable|boolean|If true, the marker receives mouse and touch events|false draggable|boolean|If true, the marker can be dragged| false fillColor|string|The symbol's fill color. All CSS3 colors are supported except for extended named colors|#4A89F3 scale|number|The width and height of the marker|12 strokeWeight|number|The symbol's stroke weight|2 strokeColor|string|The symbol's stroke color. All CSS3 colors are supported except for extended named colors|white

3. showAccuracyRadius

If true, a shape that grows with position accuracy is showed.
Default value is true.

4. watchPositionFn

Set up a watch for location changes. This function returns a number or Promise<number> to represent an integer ID that identifies the registered handler.
Default function uses navigator.geolocation.watchPosition.

Prop | Description ---- | ----------- successCallback|Required. A callback function that takes a GeolocationPosition object as an input parameter. errorCallback|Optional. An optional callback function that takes a GeolocationPositionError object as an input parameter. options|Optional. An optional PositionOptions object that provides configuration options for the location watch.

Examples

Basic

React:

import React from 'react';
import addCurrentLocation from 'google-maps-current-location'

export default function App() {

  React.useEffect(()=>{
    const mapEl = document.getElementById('map');
    if (!mapEl) {
      throw new Error('Cannot get map element');
    }

    const map = new google.maps.Map(mapEl, {
      center: new google.maps.LatLng(41.1493987, -8.1900724),
      zoom: 12,
      disableDefaultUI: true,
    });

    addCurrentLocation(map)
  
  }, []);

  return (
    <div className="App">
      <div id="map" />
    </div>
  );
}

Html:

<!DOCTYPE html>
<html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <script async
        src="https://maps.googleapis.com/maps/api/js?key=YOUR-KEY&callback=initMap&libraries=&v=weekly&lng=en"></script>
    <script src="https://unpkg.com/google-maps-current-location"></script>

    <script>
      let map;

      function initMap() {
        map = new google.maps.Map(document.getElementById("map"), {
            center: { lat: -34.397, lng: 150.644 },
            zoom: 8,
            disableDefaultUI: true,
        });

        addCurrentLocation(map)
      }
    </script>
  </head>

  <body>
    <div id="map" style="height:500px; width:500px"></div>
  </body>

</html>

Button and Marker Style


addCurrentLocation(map, {
  buttonStyle: {
    buttonPosition: google.maps.ControlPosition.TOP_LEFT,
    symbolColor: '#CE1919', // red,
    borderRadius: '50%',
  },
  markerStyle: {
    fillColor: 'green',
    scale: 10,
  }
})
  

Watch Position Function

This example implements the Capacitor Geolocation Plugin.

import {Geolocation} from '@capacitor/geolocation';
import addCurrentLocation from 'google-maps-current-location'

// ...

const watchPositionFn = async (updatePos, setError) => {
  return await Geolocation.watchPosition({enableHighAccuracy: true}, (pos, err) => {
    if (err) {
      setError(err);
      return;
    }
    updatePos(pos);
  });
}

addCurrentLocation(map, {
  watchPositionFn
})