doufei16736 2018-12-26 03:43
浏览 52

谷歌地图,在Laravel / JS的标记标签上显示数据

The problem is when I pass the data controller to blade/JS, first I am converting it to JSON(because no other way) and then pass it to da javascript. But my label is not work properly. The script code is right below.

var estates = <?php echo json_encode($estates);?>

            function initMap()
            {
                var options =
                    {
                        zoom : 10,
                        center : {lat:34.652500, lng:135.506302}
                    };

                var map = new google.maps.Map(document.getElementById('map'), options);
                for (var i=0; i < estates.length; i++) {
                @foreach ($estates as $est)
                    var marker = new google.maps.Marker({
                            map: map,
                            label: estates.price,
                            position: {
                                lat: {{$est->lat}},
                                lng: {{$est->lng}}
                            }
                     });
                @endforeach
                }
            }

So if I try to pass price is like this {{$est->price}} then I get syntax token error. Because price is varchar with Japanese characters. So I need to convert it Json first and itetare it. But when I do that, there are no markers and label in the map. Just empty map, also there is no error in the console too? If I delete the JS for loop then markers are coming but no label...

Is anyone know what am I missing here?

  • 写回答

1条回答 默认 最新

  • dsljpwi494719 2018-12-26 07:54
    关注

    Try to use new google.maps.LatLng() function:

            var estates = <?php echo json_encode($estates);?>
            //estates = <?php echo json_decode($estates);?>
            function initMap()
            {
                var options =
                    {
                        zoom : 10,
                        center : {lat:34.652500, lng:135.506302}
                    }
    
                var map = new google.maps.Map(document.getElementById('map'), options);
    
                for (var i=0;i<estates.length;i++){
                var latLng_ = new google.maps.LatLng(estates['lat'],estates['lng']);
                var marker = new google.maps.Marker({
                        map: map,
                        position: latLng_,
                        icon: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRAy10Br9W1wWCSQNPc6f9CarvNEO4qqCg1BDbe7_mYZqHwXj3v',
                        label: estates['price']
                    });
    
    
                var infoWindow = new google.maps.InfoWindow({
                    content: estates['price']
                });
    
                marker.addListener('click', function () {
                    infoWindow.open(map, marker)
                });
               }
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制