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

npm i -S @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

how to add 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

PREVIOUS NEXT
Code Example
Javascript :: convert date and time into epoch javascript 
Javascript :: random number generator javascript with range 
Javascript :: expose deployment nodeport command 
Javascript :: js check if two arrays contain same values 
Javascript :: how to use uniqid 
Javascript :: javascript keyup event enter key 
Javascript :: javascript insert element after 
Javascript :: javascript random number up to including 2 
Javascript :: javascript sort array of object by property 
Javascript :: js add text after div 
Javascript :: build apk from react native 
Javascript :: jquery convert time to 1day 2 minutes 4 seconds 
Javascript :: how to start a node server 
Javascript :: javascript regex 
Javascript :: how to pip install jsonlines 
Javascript :: chart js in angular 13 
Javascript :: file upload javascript 
Javascript :: replace object in array with another array with same id javascript 
Javascript :: javascript .foreach 
Javascript :: form data object 
Javascript :: npm run js file from command line 
Javascript :: jquery parent 
Javascript :: express req get json 
Javascript :: for loop string array javascript 
Javascript :: js loader 
Javascript :: useReducer 
Javascript :: memory leak in javascript 
Javascript :: determine if touch screen js 
Javascript :: javascript compare two arrays of objects 
Javascript :: jsonwebtoken error with react js 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =