URI: https://pottypin.appsbycarla.com/api.php?api={YOUR_API_KEY}&endpoint={ENDPOINT_OPTIONS}
ENDPOINT OPTIONS:
place_id - cross referenced to Google Places API Decoded JSON ['place_id']
Technologies Used:
PHP - A popular server-side scripting language.
- Session: PHP's built-in session handling capabilities were used to temporarily store the user's latitude and longitude across pages.
JavaScript - A client-side scripting language.
- AJAX (with jQuery): Used to send asynchronous HTTP requests to the server without reloading the page.
- Geolocation API: A web API provided by modern browsers to access the geographical location of a device.
Google Places API - A service by Google that provides information about local businesses and other points of interest.
HTML & CSS - Used for structuring and styling the web pages.
Geolocation Algorithm:
- Initialize the session and database connection: The PHP session is started to allow for storing and retrieval of session variables, and a database connection is initiated using 'db.php'.
- Check for Location Data in Session:
- Verify if the geolocation data (
lat
and lng
) is set in the session.
- If not available, the script terminates with a message "Location not set in session."
- Retrieve Nearby Places:
- Use the stored
lat
and lng
values from the session to query the Google Places API.
- Fetch nearby places of interest (e.g., restaurants, gas stations, drugstores) within a specific radius.
- Combine results from each type of place into a single array called
combinedResults
.
- Sort Combined Results:
- Use the
calculateDistance
function to measure the distance between the user's location and each place's location.
- Sort the
combinedResults
array based on distance, with the nearest places appearing first.
- Fetch Data from Local Database:
- Extract place IDs from Google's results and then query the local database (table named 'kubetas') to fetch additional details about these places.
- Merge the local database results with the
combinedResults
from the Google Places API.
- Edit Place Data:
- On accessing
edit.php
, it first connects to the database and checks for a given place ID.
- If the place ID is found in the local database, fetch and display its details. Otherwise, retrieve the place's details from the Google Places API.
- Upon form submission, check if the place already exists in the database.
- If it exists, update the record; otherwise, insert a new record into the 'kubetas' table with the provided details.
- Redirect: After updating or inserting the place details in
edit.php
, redirect the user back to the results.php
page.
Tech Stack:
- PHP: Server-side scripting language used to manage sessions, connect to the database, and perform operations.
- MySQL: The relational database used to store and retrieve place details.
- Google Places API: External API used to fetch nearby places based on geolocation data.
- cURL: A PHP library used to make HTTP requests, specifically to fetch data from the Google Places API.