【问题标题】:laravel 5 loading addresses in google map apilaravel 5在谷歌地图api中加载地址
【发布时间】:2015-11-15 17:05:44
【问题描述】:

我是 json 和 laravel 的新手,我想提出我的问题。

我有一个名为 dealer_address 的数据库表,其中包含 id、address、state、pin、city。

我用一些地址加载地图的javascript是:

<script type="text/javascript">
    $(function(){
        $('#test1').gmap3({
            map:{
                options:{
                    center:[46.578498,2.457275],
                    zoom: 5
                }
            },
            marker:{
                values:[
                    {latLng:[48.8620722, 2.352047], data:"Paris !"},
                    {address:
                        $.ajax({
                            url: '/maps2/',
                            type: 'get',
                            data: {_token: CSRF_TOKEN},
                            dataType: 'JSON',
                            success: function (results) {
                                console.log(results);
                            }
                        })}
                ],
                options:{
                    draggable: false
                },
                events:{
                    mouseover: function(marker, event, context){
                        var map = $(this).gmap3("get"),
                            infowindow = $(this).gmap3({get:{name:"infowindow"}});
                        if (infowindow){
                            infowindow.open(map, marker);
                            infowindow.setContent(context.data);
                        } else {
                            $(this).gmap3({
                                infowindow:{
                                    anchor:marker,
                                    options:{content: context.data}
                                }
                            });
                        }
                    },
                    mouseout: function(){
                        var infowindow = $(this).gmap3({get:{name:"infowindow"}});
                        if (infowindow){
                            infowindow.close();
                        }
                    }
                }
            }
        })
    });
</script>

但是,我想根据数据库结果在地图上显示地址。

public function ShowData()
{
    $location = '700016';
    $results = DB::table('dealer_address')
        ->where('dealer_address.city', 'LIKE', '%'.$location.'%')
        ->orWhere('dealer_address.pincode', 'LIKE', '%'.$location.'%')
        ->orWhere('dealer_address.state', 'LIKE', '%'.$location.'%')
        ->get();

    dd($results);
    return View::make('/maps2')->with('data', json_encode($results));

}

我的dd($results) 工作正常。我需要在我的 javascript 中更改什么位置和什么来传递这个 json ?

【问题讨论】:

    标签: javascript php json google-maps laravel


    【解决方案1】:

    我相信您可以将您的退货声明简化为:

    return json_encode($results->toArray());
    

    【讨论】:

    • 感谢您的评论,但我已经处理好了。我的问题是,我应该在视图刀片上的哪里更改 JAVASCRIPT 以捕获 json 以及如何。我是 JSON 新手
    【解决方案2】:

    Laravel 4.2

    return Response::json($results->toArray());
    

    Laravel 5.1

    return response()->json($results->toArray());
    

    你可以创造

    var options = [your data]
    

    之后打电话$('#test1').gmap3(options)

    当你有options 您可以为json数据调用ajax并设置它

    options.marker.values.address[0] = response
    

    【讨论】:

    • 感谢您的评论,但我已经处理好了。我的问题是,我应该在视图刀片上的哪里更改 JAVASCRIPT 以捕获 json 以及如何。我是 JSON 新手
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多