【问题标题】:How to Display Multiple Gmaps Markers in Laravel 8?如何在 Laravel 8 中显示多个 Gmaps 标记?
【发布时间】:2022-01-26 09:14:04
【问题描述】:

我想在仪表板上显示多个 gmaps 标记。此标记数据,取自数据库。我已经这样编码了,但它仍然不起作用。

控制器

public function index()
{
    $markers = Report::select('location','latitude','longitude')->get()->toArray();
    
    // dd($markers);
    return view('dashboard', compact('markers'));
}

在刀片中,我是这样制作的。这是我从google documentation得到的参考

刀片

@section('styles')
<style>
    /* Set the size of the div element that contains the map */
    #map {
    height: 400px;
    /* The height is 400 pixels */
    width: 100%;
    /* The width is the width of the web page */
    }
</style>
<script>

function initMap() {
    const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 10,
        center: { lat: -6.261493, lng: 106.810600 },
});

setMarkers(map);
}

const locations = <?php print_r(json_encode($markers)) ?>;

function setMarkers(map) {

const image = {
    url: "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png",
    // This marker is 20 pixels wide by 32 pixels high.
    size: new google.maps.Size(20, 32),
    // The origin for this image is (0, 0).
    origin: new google.maps.Point(0, 0),
    // The anchor for this image is the base of the flagpole at (0, 32).
    anchor: new google.maps.Point(0, 32),
};

const shape = {
    coords: [1, 1, 1, 20, 18, 20, 18, 1],
    type: "poly",
};

for (let i = 0; i < locations.length; i++) {
    const location = locations[i];

    new google.maps.Marker({
    position: { lat: parseFloat(location[1]), lng: parseFloat(location[2]) },
    map,
    icon: image,
    shape: shape,
    title: location[0],
    zIndex: location[3],
    });

    console.log(locations[i]);
    }
}

</script>

@endsection

...
...
<div class="col-12">
    <div class="card">
         <div class="card-body">
               <div id="map"></div>
         </div>
    </div>
</div>
...
...

如果我查看控制台,输出显示如下。

Console.log

如何解决?谢谢你

【问题讨论】:

    标签: javascript php laravel api google-maps


    【解决方案1】:

    已解决。这种情况可以通过在控制器中使用集合映射来解决

        public function index()
        {
            $markers = Report::select('location','latitude','longitude')->get();
    
            $markers = $markers->map(function ($item, $key){
                return [$item->location, $item->latitude, $item->longitude];
            });
    
            return view('dashboard', compact('markers'));
        }
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 2012-03-20
      • 2011-10-15
      • 2017-01-24
      相关资源
      最近更新 更多