1.5.0 • Published 4 years ago

sedyain-react-location-picker v1.5.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

react-location-picker

A react component to pick a location using google maps.

Demo

Installation

npm install react-location-picker --save

or

yarn add react-location-picker --save

You need to include google map script in your page.
<script src="https://maps.googleapis.com/maps/api/js?key=<INSERT_API_KEY_HERE>&v=3.exp&libraries=geometry,drawing,places"></script>

Example

import React, { Component } from 'react';
import LocationPicker from 'react-location-picker';

/* Default position */
const defaultPosition = {
  lat: 27.9878,
  lng: 86.9250
};


class LocationPickerExample extends Component {
  constructor (props) {
    super(props);

    this.state = {
      address: "Kala Pattar Ascent Trail, Khumjung 56000, Nepal",
      position: {
         lat: 0,
         lng: 0
      }
    };

    // Bind
    this.handleLocationChange = this.handleLocationChange.bind(this);
  }

  handleLocationChange ({ position, address, places }) {

    // Set new location
    this.setState({ position, address });
  }

  render () {
    return (
      <div>
        <h1>{this.state.address}</h1>
        <div>
          <LocationPicker
            mapContainerStyle={{height: '200px', width: '400px'}}
            defaultPosition={this.state.defaultPosition}
            onChange={this.handleLocationChange}
          />
        </div>
      </div>
    )
  }
}

Usage

LocationPicker properties

PropertyTypeDescription
mapContainerStyleobjectrequired, A object with height and width who is passed to the map
onChangefunctionrequired, A callback which gets called on every map marker position change, it is passed with one argument of type object which has location information.
defaultPositionobjectrequired, A default position for map center.
zoomnumberoptional, Map zoom level
radiusnumberoptional, Circle radius in meter. or Pass -1 to hide it.
circleOptionsobjectoptional, https://developers.google.com/maps/documentation/javascript/3.exp/reference#CircleOptions
mapOptionsobjectoptional, https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions

Development

For demo, clone this repo and run

Install all dependencies
yarn

Start
yarn start

Open http://localhost:8080 in your browser.

1.5.1

4 years ago

1.5.0

4 years ago