phayes0289's avatar

Showing Nearby Hydrants on A Map based on the Usrs Current Location

I have a Laravel project that has a model named “Hydrant” that has over 2000 hydrants in its database.

I need to display a Google Map that zooms in on the users current location and displays any hydrants from the database in the current viewport. I do have a Google Maps account. I am not sure how to pass the user’s current latitude and longitude to the “map” method in the controller.

My goal is to have the map show the users current location in the center of the screen with a blue dot or similar surrounded by icons representing the nearby hydrants.

When the hydrant is clicked, i need it display an info window that provides some basic info about the hydrant from the Hydrant model. Below that data, there should be two bootstrap buttons that will be links to other blade pages. One should be labeled as "Photo". The other as "Inspection".

This is what I have so far, but it is not working at all. Using Laravel best practices, how can I re-write this do accomplish my goals? I know I need to somehow warn the user that we are asking for their location.

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve your goal of displaying nearby hydrants on a Google Map based on the user's current location, you can follow these steps. I'll provide a complete solution, including the necessary Laravel controller, route, and JavaScript code.

Step 1: Set Up Your Laravel Route and Controller

First, create a route and a controller method to fetch hydrants within the current viewport.

web.php

use App\Http\Controllers\HydrantController;

Route::get('/hydrants', [HydrantController::class, 'index']);

HydrantController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Hydrant;

class HydrantController extends Controller
{
    public function index(Request $request)
    {
        $ne_lat = $request->input('ne_lat');
        $ne_lng = $request->input('ne_lng');
        $sw_lat = $request->input('sw_lat');
        $sw_lng = $request->input('sw_lng');

        $hydrants = Hydrant::whereBetween('latitude', [$sw_lat, $ne_lat])
                            ->whereBetween('longitude', [$sw_lng, $ne_lng])
                            ->get();

        return response()->json($hydrants);
    }
}

Step 2: Create the Blade View

Create a Blade view to display the map and include the necessary JavaScript.

map.blade.php

Explanation

  1. Laravel Route and Controller: The route /hydrants calls the index method of HydrantController, which fetches hydrants within the specified latitude and longitude bounds.

  2. Blade View: The map.blade.php file includes the Google Maps API and initializes the map. It uses the Geolocation API to get the user's current location and centers the map on it. It then fetches hydrants within the current viewport and displays them as markers on the map.

  3. JavaScript: The JavaScript code handles map initialization, fetching hydrants, and displaying them with info windows. It also includes error handling for geolocation and a function to clear existing markers before adding new ones.

Replace YOUR_API_KEY with your actual Google Maps API key.

This solution should meet your requirements and display nearby hydrants on a Google Map based on the user's current location.

Please or to participate in this conversation.