Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

google map in react js

import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';

const AnyReactComponent = ({ text }) => <div>{text}</div>;

class SimpleMap extends Component {
  static defaultProps = {
    center: {
      lat: 59.95,
      lng: 30.33
    },
    zoom: 11
  };

  render() {
    return (
      // Important! Always set the container height explicitly
      <div style={{ height: '100vh', width: '100%' }}>
        <GoogleMapReact
          bootstrapURLKeys={{ key: /* YOUR KEY HERE */ }}
          defaultCenter={this.props.center}
          defaultZoom={this.props.zoom}
        >
          <AnyReactComponent
            lat={59.955413}
            lng={30.337844}
            text="My Marker"
          />
        </GoogleMapReact>
      </div>
    );
  }
}

export default SimpleMap;
Comment

react google map

npm install --save google-map-react
Comment

@react-google-maps/api

with NPM 

npm i -S @react-google-maps/api 

or 

Yarn 
yarn add @react-google-maps/api
Comment

react google map api

import React from 'react'
import { GoogleMap, useJsApiLoader } from '@react-google-maps/api';

const containerStyle = {
  width: '400px',
  height: '400px'
};

const center = {
  lat: -3.745,
  lng: -38.523
};

function MyComponent() {
  const { isLoaded } = useJsApiLoader({
    id: 'google-map-script',
    googleMapsApiKey: "YOUR_API_KEY"
  })

  const [map, setMap] = React.useState(null)

  const onLoad = React.useCallback(function callback(map) {
    // This is just an example of getting and using the map instance!!! don't just blindly copy!
    const bounds = new window.google.maps.LatLngBounds(center);
    map.fitBounds(bounds);

    setMap(map)
  }, [])

  const onUnmount = React.useCallback(function callback(map) {
    setMap(null)
  }, [])

  return isLoaded ? (
      <GoogleMap
        mapContainerStyle={containerStyle}
        center={center}
        zoom={10}
        onLoad={onLoad}
        onUnmount={onUnmount}
      >
        { /* Child components, such as markers, info windows, etc. */ }
        <></>
      </GoogleMap>
  ) : <></>
}

export default React.memo(MyComponent)
Comment

PREVIOUS NEXT
Code Example
Javascript :: disable textarea angular 
Javascript :: d3.js onclick event 
Javascript :: if text is present make div hide 
Javascript :: mongoose get elements that contain substring 
Javascript :: how to access items inside anonymous object 
Javascript :: node.js upload files 
Javascript :: how to check if username already exists in database using javascript 
Javascript :: Remove Array Duplicate 
Javascript :: react firebase add doc to collection 
Javascript :: persistent bugger javascript code 
Javascript :: can we add two functions onclick event 
Javascript :: react native dropdown 
Javascript :: js opposite of startswith 
Javascript :: how to disable menu bar in browser using javascript 
Javascript :: javascript how-do-i-check-whether-a-checkbox-is-checked-in-jquery 
Javascript :: How to append the string to the current url in jquery | Javascript 
Javascript :: formula regex para validar cpf e cnpj no google forms 
Javascript :: axios put request 
Javascript :: how to combine two regular expressions in javascript 
Javascript :: redis to promise 
Javascript :: react-particles-js not working 
Javascript :: javascript filter method arrow function 
Javascript :: javascript Iterate Sets 
Javascript :: express generator error handling 
Javascript :: forece reload without clear cache js 
Javascript :: chrome resize window javascript 
Javascript :: react spread operator 
Javascript :: js multiline string with variables 
Javascript :: get all a tags javascript 
Javascript :: click binding angular 8 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =