Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapbox API #415

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions New_APIs/Mapbox API/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 🗺️ Mapbox API

## Overview

The **Mapbox API** offers customizable and scalable map services, including real-time traffic data, geocoding, and advanced map visualizations. It is useful for integrating location-based services into your applications with rich map functionalities and interactive features.

## Features

- **Customizable Maps**: Create and style maps to fit the design of your application.
- **Real-Time Traffic Data**: Access up-to-date traffic information for better route planning.
- **Geocoding**: Convert addresses into geographic coordinates and vice versa.
- **Advanced Map Visualizations**: Display various data layers and visualizations on the map.
- **Routing**: Calculate and display routes with options for different travel modes.

## Getting Started

### Prerequisites

- **Mapbox Access Token**: You need to sign up for a Mapbox account and obtain an access token. You can get your access token by visiting the [Mapbox account page](https://account.mapbox.com/).

### Installation

To use the Mapbox API in a Node.js application, you can use the `mapbox-gl` library for map visualization and `node-fetch` for making HTTP requests:

```bash
npm install mapbox-gl node-fetch
```

## Contributed by
### Revanth
24 changes: 24 additions & 0 deletions New_APIs/Mapbox API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Mapbox Explorer</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.css" rel="stylesheet">
</head>
<body>
<header>
<h1>Mapbox Explorer</h1>
</header>
<main>
<div id="map"></div>
<div class="controls">
<input type="text" id="locationInput" placeholder="Search for a location">
<button id="searchButton">Search</button>
</div>
</main>
<script src="index.js"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions New_APIs/Mapbox API/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN'; // Replace with your Mapbox Access Token

const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-74.5, 40], // Default center [longitude, latitude]
zoom: 9 // Default zoom level
});

document.getElementById('searchButton').addEventListener('click', () => {
const location = document.getElementById('locationInput').value;
geocodeLocation(location);
});

function geocodeLocation(location) {
const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(location)}.json?access_token=${mapboxgl.accessToken}`;

fetch(url)
.then(response => response.json())
.then(data => {
if (data.features && data.features.length > 0) {
const coordinates = data.features[0].geometry.coordinates;
map.flyTo({ center: coordinates, zoom: 12 });
new mapboxgl.Marker().setLngLat(coordinates).addTo(map);
} else {
alert('Location not found');
}
})
.catch(error => {
console.error('Error fetching location:', error);
alert('Failed to fetch location.');
});
}
13 changes: 13 additions & 0 deletions New_APIs/Mapbox API/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions New_APIs/Mapbox API/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "mapbox-explorer",
"version": "1.0.0",
"description": "A simple app to explore locations using the Mapbox API",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "Your Name",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mapbox-explorer": "file:"
}
}
8 changes: 8 additions & 0 deletions New_APIs/Mapbox API/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const app = express();

app.use(express.static('public'));

app.listen(3000, () => {
console.log('Server is running on port 3000');
});
69 changes: 69 additions & 0 deletions New_APIs/Mapbox API/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #1e3c72, #c5573b);
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}

header {
text-align: center;
margin-bottom: 20px;
}

header h1 {
font-size: 3em;
color: #fff;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.6);
}

main {
width: 90%;
max-width: 1000px;
height: 80%;
position: relative;
}

#map {
width: 100%;
height: 100%;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.controls {
position: absolute;
top: 10px;
left: 10px;
display: flex;
flex-direction: row;
gap: 10px;
}

.controls input {
padding: 10px;
font-size: 1em;
border: none;
border-radius: 5px;
width: 200px;
outline: none;
}

.controls button {
padding: 10px 15px;
font-size: 1em;
background-color: #444;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}

.controls button:hover {
background-color: #666;
}