1.0.3 • Published 14 days ago

react-gmap-picker v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

react-gmap-picker

React google maps location picker/marker with Advanced Marker

Table of Contents

Installation

Install react-gmap-picker and its dependeices

npm install --save react-gmap-picker

or

yarn add react-gmap-picker

Usage/Examples

import { useState } from 'react';
import { Picker } from 'react-gmap-picker';
import React from 'react';

const INITIAL_LOCATION = { lat: 13.4, lng: 77.0 };
const INITIAL_ZOOM = 10;

const API_KEY = process.env.API_KEY as string;

const App = () => {
  const [defaultLocation, setDefaultLocation] = useState(INITIAL_LOCATION);
  const [location, setLocation] = useState(defaultLocation);
  const [zoom, setZoom] = useState(INITIAL_ZOOM);

  const handleChangeLocation = (lat: number, lng: number) => {
    setLocation({ lat, lng });
  };

  const handleChangeZoom = (newZoom: number) => {
    setZoom(newZoom);
  };

  const handleResetLocation = () => {
    setDefaultLocation({ ...INITIAL_LOCATION });
    setLocation({ ...INITIAL_LOCATION });
    setZoom(INITIAL_ZOOM);
  };

  return (
    <div>
      <button onClick={handleResetLocation}>Reset Location</button>
      <label>Latitude:</label>
      <input type="text" value={location.lat} disabled />
      <label>Longitude:</label>
      <input type="text" value={location.lng} disabled />
      <label>Zoom:</label>
      <input type="text" value={zoom} disabled />

      <div>
        <h4>Map 1 (roadmap)</h4>
        <Picker
          defaultLocation={defaultLocation}
          zoom={zoom}
          mapTypeId="roadmap"
          style={{ height: '700px' }}
          onChangeLocation={handleChangeLocation}
          onChangeZoom={handleChangeZoom}
          apiKey={API_KEY}
        />
      </div>
    </div>
  );
};

export default App;

Props

ParameterTypeDefaultDescription
apiKeystringRequiredRequired. Google maps API key
defaultLocation{lat: number; lng: number}RequiredRequired. Default coordinate.
zoom{lat: number; lng: number}7Default coordinate.
onChangeLocation(lat: number, lng: number) => voidnullExecutes when location changes.
onChangeZoom(zoom: number) => voidnullExecutes when room level changes.
styleany{ width: '100%', height: '600px' }Map container style.
classNamestringundefinedMap className.
mapTypeIdgoogle.maps.MapTypeIdundefinedMap type you want to see.
1.0.3

14 days ago

1.0.2

15 days ago

1.0.1

15 days ago

0.3.2

11 months ago

0.3.1

11 months ago

0.2.2

11 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago