Giter Club home page Giter Club logo

react-map-scaffold's Introduction

React + Google Maps Coding Exercise

Install

  1. npm install
  2. Rename pages/_google-maps-api-key.js to pages/google-maps-api-key.js and insert your Google API Key or ask Asa for one.
  3. npm run dev

Exercises

1. Make the map bigger

  1. Open up pages/components/maps.js
  2. Find the map dimensions variable and increase the size.
const containerStyle = {
  width: '400px',
  height: '400px'
};

2. Choose a different map center point

Find the map center variable and change the coordinates.

3. Choose a different map zoom level

Find the map zoom variable and change the coordinates.

4. Add a marker when you click the map

Add the following code at the top of the MapComponent function.

const [location, setLocation] = useState({
  lat: 0,
  lng: 0,
});

const onClickMap = (event) => {
  const location = event.latLng.toJSON();
  setLocation(location);
}

Then add the following code inside the <GoogleMap> component:

<Marker position={location} />

Now click a location on the map.

5. Keep a running list of markers and locations

Replace the code you just added with:

const [locations, setLocations] = useState([]);

const rows = locations.map((location) => {
  return <li key={`${location.lat},${location.lng}`}>
    {location.lat}, {location.lng}
  </li>;
});
const markers = locations.map((location) => {
  const key = `${location.lat}, ${location.lng}`;
  return <Marker key={key} position={location} />;
});

const onClickMap = (event) => {
  const location = event.latLng.toJSON();
  setLocations(locations.concat([location]));
}

Now use the markers and rows variables to actually render the information.

Next Steps

react-map-scaffold's People

Contributors

asakusuma avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.