带有谷歌地图的 Laravel 未在数据库中显示纬度和经度值的标记

我正在尝试根据 mysql 数据库中已存储的值(纬度和经度)在 Google 地图上显示标记。但是当我尝试加载它时,谷歌地图中没有显示标记。我是 laravel 的新手。请帮我实现它。

我将<?php echo $loc->lat ?> & <?php echo $loc->long ?>在同一页面中获取纬度和经度值,但我不知道如何传递它,请帮助我



慕盖茨4494581
浏览 124回答 2
2回答

慕虎7371278

由于您的脚本位于 Laravel 刀片内部,因此您可以将 PHP 变量经度和纬度分配给 javascript 变量。//Suppose your PHP variable name is $loc having lat and long attribute<script>&nbsp; &nbsp; let provided_longitude = <?php echo $loc->long; ?>;&nbsp; &nbsp; let provided_latitude = <?php echo $loc->lat; ?>;&nbsp; &nbsp; console.log(provided_longitude, provided_latitude);</script>现在您可以在谷歌地图中使用提供的经度和提供的纬度。example.blade.php 中的iframe 示例<iframe&nbsp; &nbsp; width="600"&nbsp; &nbsp; height="450"&nbsp; &nbsp; frameborder="0" style="border:0"&nbsp; &nbsp; src="https://www.google.com/maps/embed/v1/place?key=YOUR_API_KEY&nbsp; &nbsp; &q={{$loc->lat}},{{$loc->long}}" allowfullscreen></iframe>编辑 1显示表格中的标记(例如:具有列经度、纬度的订单)//在你的控制器里面$orders = Order::select('id', 'longitude', 'latitude');return view('your.page', [&nbsp; &nbsp; 'orders' => $orders]);&nbsp;//在你的/page.blade.php里面<div id="map" style="width: 500px; height: 400px;"></div>...<script src="http://maps.google.com/maps/api/js?sensor=false"&nbsp; &nbsp; &nbsp; &nbsp; type="text/javascript"></script><script type="text/javascript">&nbsp; &nbsp; var locations = <?php echo $orders ?>;&nbsp; &nbsp; var map = new google.maps.Map(document.getElementById('map'), {&nbsp; &nbsp; &nbsp; &nbsp; zoom: 10,&nbsp; &nbsp; &nbsp; &nbsp; center: new google.maps.LatLng(-33.92, 151.25),&nbsp; &nbsp; &nbsp; &nbsp; mapTypeId: google.maps.MapTypeId.ROADMAP&nbsp; &nbsp; });&nbsp; &nbsp; var infowindow = new google.maps.InfoWindow();&nbsp; &nbsp; var marker, i;&nbsp; &nbsp; for (i = 0; i < locations.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; marker = new google.maps.Marker({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: new google.maps.LatLng(locations[i]['latitude'], locations[i]['longitude']),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map: map&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }</script>

MYYA

您必须在 javascript 的标记对象中传递您的数据库 lat long 值。var marker = new google.maps.Marker({ map: map, position: point,(这里你必须传递你从数据库中获取的 lat 和 long 的动态值) label: icon.label });作为参考,您可以抛出以下链接。&nbsp;如何在地图php上显示标记
打开App,查看更多内容
随时随地看视频慕课网APP