GithubHelp home page GithubHelp logo

ninjas-code-official / geolocation-interactive-delivery-zones-mapping Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 1.86 MB

This is our geolocation enabled zones mapping module. With this module you can set delivery zones for riders and restaurants. This is just one part of our entire food delivery solution Enatega. For more information, visit www.enatega.com

Home Page: http://www.enatega.com

HTML 15.35% CSS 28.13% JavaScript 56.52%
geolocation location-mapping web-server delivery-zones

geolocation-interactive-delivery-zones-mapping's Introduction

Interactive Zone Mapping with Google Maps API

YouTube Channel Company Website

Static Badge Static Badge Static Badge

๐Ÿ”ฅ Introduction

This guide demonstrates the process of building a geolocation enabled zones mapping module using React and the Google Maps API. The objective is to enable users to create, edit, and delete delivery zones on the map.

โฎ๏ธ Prerequisites

Before proceeding, make sure you have the following:

  1. Basic knowledge of HTML, CSS, and JavaScript.
  2. Node.js and npm installed on your machine.
  3. A Google Maps API key.

๐Ÿ”‚ Getting Started

1. Create a new React app:

npx create-react-app map-app
cd map-app

2. Install dependencies:

Install the required dependencies for Google Maps:

npm install @react-google-maps/api

๐Ÿ—บ๏ธ Setting up the Interactive Map

1. Obtain a Google Maps API Key:

Obtain a Google Maps API key by following the official documentation

2. Implementing the Map Container:

Create the MapContainer component. This component will handle map interactions, zone creation, and editing.

const MapContainer = ({ zones, setZones }) => {
  const [map, setMap] = useState(null);
  const [drawing, setDrawing] = useState(false);
  const [newZone, setNewZone] = useState([]);

  const handleMapLoad = (map) => {
    setMap(map);
  };

  const handleMapClick = (e) => {
    if (drawing) {
      setNewZone((prevZone) => [...prevZone, { lat: e.latLng.lat(), lng: e.latLng.lng() }]);
    }
  };

  const handleStartDrawing = () => {
    setDrawing(true);
    setNewZone([]);
  };

  const handleFinishDrawing = () => {
    setDrawing(false);

    // Save the drawn zone to state
    setZones((prevZones) => [...prevZones, { name: 'New Zone', description: 'Description', polygon: newZone }]);

    // Optionally, you can save the zones to local storage here
    // localStorage.setItem('zones', JSON.stringify([...prevZones, { name: 'New Zone', description: 'Description', polygon: newZone }]));

    setNewZone([]);
  };

  return (
    <div className="container">
      <h1>Add ZONE</h1>
      <LoadScript googleMapsApiKey="YOUR_GOOGLE_MAPS_API_KEY">
        <GoogleMap
          id="map"
          mapContainerStyle={{ height: '400px', width: '100%' }}
          zoom={8}
          center={{ lat: 0, lng: 0 }}
          onLoad={handleMapLoad}
          onClick={handleMapClick}
        >
          {drawing && <Polygon path={newZone} options={{ fillColor: '#00FF00', fillOpacity: 0.35 }} />}
        </GoogleMap>
      </LoadScript>

      {/* ... (other UI elements) */}
    </div>
  );
};

export default MapContainer;

โž— Let's break down the zone creation functions:

handleMapLoad Function:

This function is triggered when the Google Map is loaded. It sets the map state to the loaded map instance.

handleMapClick Function:

This function is triggered when the map is clicked. If the drawing state is true, it adds a new point (latitude and longitude) to the newZone state array.

handleStartDrawing Function:

This function sets the drawing state to true and initializes an empty newZone array when the user wants to start drawing a new zone.

handleFinishDrawing Function:

This function is called when the user finishes drawing a zone. It sets the drawing state to false, adds the drawn zone (with a default name, description, and polygon) to the zones state array, and optionally saves the zones to local storage.

These functions work together to enable the creation of new zones on the map by clicking and dragging. The drawn zones are then saved to the state or local storage for later use.

๐Ÿ“ˆ Learn More:

This map functionality is used in our full fledged project Enatega; explore this project to learn more. Learn more about the Google Maps API to incorporate advanced functionalities by visiting our Multivendor repo: Food-delivery-multivendor

Check out the live demo of this project.Live Demo

๐ŸŒ Contact Us:

To acquire the backend license of the full solution go to Enatega.

geolocation-interactive-delivery-zones-mapping's People

Contributors

muhammadadnan111 avatar thekashif avatar adnan909 avatar

Stargazers

Clayton Kehoe avatar

Watchers

Sharan avatar  avatar

Forkers

thekashif

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.