0.2.3 • Published 3 years ago

google-maps-current-location v0.2.3

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

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.

PropTypeDescriptionDefault
buttonPositiongoogle.maps.ControlPositionPosition of button in relation with map. Check here to find out more.google.maps.ControlPosition.RIGHT_BOTTOM
mainMarginstringThe button's margin10px
backgroundColorstringThe button's background color. All CSS3 colors are supported#fff
symbolColorstringThe symbol's fill color. Only HEX color supported#6F6F6F
borderstringThe button's border0px
borderRadiusstringThe button's border radius0px
outlinestringThe button's outline0px
heightstringThe button's height (height===width)40px
boxShadowstringThe button's boxShadow0 1px 4px rgba(0,0,0,0.3)
cursorstringMouse cursor type to show on hoverpointer

2. markerStyle

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

PropTypeDescriptionDefault
clickablebooleanIf true, the marker receives mouse and touch eventsfalse
cursorstringMouse cursor type to show on hoverpointer
draggablebooleanIf true, the marker can be draggedfalse
fillColorstringThe symbol's fill color. All CSS3 colors are supported except for extended named colors#4A89F3
scalenumberThe amount by which the symbol is scaled in size6
strokeWeightnumberThe symbol's stroke weight2
strokeColorstringThe symbol's stroke color. All CSS3 colors are supported except for extended named colorswhite

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.

PropDescription
successCallbackRequired. A callback function that takes a GeolocationPosition object as an input parameter.
errorCallbackOptional. An optional callback function that takes a GeolocationPositionError object as an input parameter.
optionsOptional. 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
})
0.2.1

3 years ago

0.2.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.3

3 years ago

0.1.0

3 years ago