0.0.15 • Published 4 years ago

react-google-maps-ts v0.0.15

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

react-google-maps

React components library for google maps.

Library is under active development. It is not recomended for production usage so far.

import {GoogleApiProvider, Map, Marker} from 'react-google-maps-ts';

const App = () => (
  <GoogleApiProvider
    apiKey="<API_KEY>"
  >
    <MyAwesomeApp />
  </GoogleApiProvider>
);

const geoPoint = {lat: 50, lng: 50};

const MyAwesomeApp = () => (
  <Map
    defaultCenter={geoPoint}
  >
    <Marker 
      position={geoPoint}
      zoom={8}
      title="My perfect marker"
    />
  </Map>
);
Implemented features:
  • Marker
  • Polygon
  • Polyline
  • Circle
  • Rectangle
  • TilesOverlay (MapType)
  • DomOverlay (OverlayView)
  • GroundOverlay
  • InfoWindow

GoogleMapsProvider

Common API loading

import {GoogleApiProvider} from 'react-google-maps-ts';

const App = () => (
  <GoogleApiProvider
    apiKey="<API_KEY>"
    geometry // include geometry library
    drawing // include drawing library
    places // include places library
    visualization // include visualization library
    language='en' // Google Maps localization
    region='GB'
    version='quarterly' // Google Maps versioning
  >
    <MyAwesomeApp />
  </GoogleApiProvider>
);

Proxy API loading

You can load Google API using your own proxy

import {GoogleApiProvider} from 'react-google-maps-ts';

const App = () => (
  <GoogleApiProvider
    proxy='<Your proxy URL>'
  >
    <MyAwesomeApp />
  </GoogleApiProvider>
);

Custom loading callback

You can load Google API using your own loading method

import {GoogleApiProvider} from 'react-google-maps-ts';

const load = async (): Promise<GoogleObject> => {
  // implementation

  return googleApi;
}

const App = () => (
  <GoogleApiProvider
    load={load}
  >
    <MyAwesomeApp />
  </GoogleApiProvider>
);

Extending library with you own classes

You can extend Google API with your custom objects

import {GoogleApiProvider} from 'react-google-maps-ts';

const extend = async (google: Google): void => {
  google["<your namespace>"] = // your extension;
}

const App = () => (
  <GoogleApiProvider
    apiKey="<API_KEY>"
    extend={extend}
  >
    <MyAwesomeApp />
  </GoogleApiProvider>
);

Smart vs Dumb components

Main purpose of this library is combining of declarative React syntax with free accessibility to Google Maps objects without doubtful workarounds.

Each component has smart and dumb implementations.

Smart components

Smart components are self-contained. They have their own encapsulated Google Maps objects and you can't access them. But they are extremely simple to use and their intarfaces are similar to corresponding Google Maps object.

import {Map, Marker} from 'react-google-maps-ts';

const geoPoint = {lat: 50, lng: 50};

const MyAwesomeApp = () => (
  <Map
    defaultCenter={geoPoint}
  >
    <Marker 
      position={geoPoint}
      zoom={8}
      title="My perfect marker"
    />
  </Map>
);

Dumb components

Dumb components use Google Maps objects from external context. So you can access it if you need.

import {DumbMap, withSmartMapCtx, MapService, useGoogleCtx} from 'react-google-maps-ts';

interface AwesomeAppProps {
  ...
};

const geoPoint = {lat: 50, lng: 50};

const MyAwesomeApp = (
  {mapService}: AwesomeAppProps & {mapService: MapService | undefined}
) => {  
  const googleApi = useGoogleCtx();

  const centerMap = () => { 
    // Notice that such approach creates new function on each render
    // It is provided only as example. It is recomended to avoid it.

    if (!mapService || !googleApi) return;

    const {object} = mapService; // Google Maps object;

    const defaultCenter = new googleApi.maps.LatLng(geoPoint.lat, geoPoint.lng);

    object.setCenter(defaultCenter);
  }

  return (
    <>
      <button onClick={centerMap}>Center map</button>
      <DumbMap
        defaultCenter={geoPoint}
      />
    </>
  );
}

withSmartMapCtx<AwesomeAppProps>(MyAwesomeApp);
0.0.14

4 years ago

0.0.15

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.13

4 years ago

0.0.9

4 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago