LarsWiegers / laravel-maps

Your laravel maps libary.
https://github.com/LarsWiegers/laravel-maps
MIT License
254 stars 35 forks source link

Cant create dynamic markers #51

Closed matb987 closed 6 months ago

matb987 commented 6 months ago

an you give me an example of how you would make this loop as i keep getting the error: Undefined array key 0 controller code:

$drivers = User::where('status', '=', 'active')->get();
//array for drivers location without key
$markers = [];
foreach ($drivers as $driver) {

            $markers[] = [
                'lat' => $driver->latitude,
                'long' => $driver->longitude,
            ];

    }

View Code

<x-maps-leaflet :markers="$markers" :centerPoint="['lat' => 51.7792614, 'long' => 3.212754]" :centerPoint="['lat' => 51.7792614, 'long' => 3.212754]"></x-maps-leaflet>
matb987 commented 6 months ago

image

image

LarsWiegers commented 6 months ago
<x-maps-leaflet :zoomLevel="6"
                        :centerPoint="['lat' => 52.16444513293423, 'long' => 5.985622388024091]"
                        :markers="[['lat' => 52.16444513293423, 'long' => 5.985622388024091, 'icon' => 'https://cdn0.iconfinder.com/data/icons/small-n-flat/24/678111-map-marker-512.png', 'iconSizeX' => 32, 'iconSizeY' => 32]]"
        ></x-maps-leaflet>

Can you try this?

matb987 commented 6 months ago

how can i make it so it can get the infomation from the database ? eg using a variable

matb987 commented 6 months ago

here is my current code if it helps

$drivers = DB::table('users')->where('status', 'active')->get();
        //list for drivers location without key
        //set array values
        $markers = [];
        foreach ($drivers as $driver) {
            $markers[] = [
                'lat' => $driver->latitude,
                'long' => $driver->longitude,
                'infoWindow' => [
                    'content' => $driver->name,
                ],
            ];

        }

        return view('livewire.driver-map', compact('drivers', 'markers'));
matb987 commented 6 months ago

it works with the code

  for ($i = 0; $i < 3; $i++) {
            $markers[] = [
                'lat' => 51 + $i,
                'long' => -3 + $i
            ];
        }

but not with a foreach loop like this

 foreach ($drivers as $driver) {
            $markers[] = [
                'lat' => $driver->lat,
                'long' => $driver->long,
            ];
        }
matb987 commented 6 months ago

I got it working with the following code. not sure if it is the best way but it works

$drivers = DB::table('users')->where('status', 'active')->get();
        //create array
        $markers = [];
        foreach ($drivers as $driver) {
            //if the driver has a location
            if ($driver->latitude != null && $driver->longitude != null) {
                //get the latitude and longitude
                $lat = $driver->latitude;
                //convert to float
                $lat = (float)$lat;
                $long = $driver->longitude;
                //convert to float
                $long = (float)$long;

               //append to push array
                array_push($markers, ['lat' => $lat, 'long' => $long]);
            }

        }
LarsWiegers commented 6 months ago

Happy it works now :)