Javascript 未捕获类型错误:无法读取属性';77.597639984451';未定义的

Javascript 未捕获类型错误:无法读取属性';77.597639984451';未定义的,javascript,google-maps,google-maps-api-3,laravel-5.2,Javascript,Google Maps,Google Maps Api 3,Laravel 5.2,我知道我在问一个类似的问题,这个问题已经有很多答案了。但我无法找到解决我问题的办法。 这里我使用的是Laravel5.2,我试图从mysql数据库中获取latlng并显示在地图中。我正在使用谷歌地图api v2。我的确切问题是,我在控制台中获取latlng值,但我无法在地图上显示它。这里我附上完整的代码。任何帮助都将是巨大的 这是我的MapController.php class MapController extends Controller { public $type = 'Map'; p

我知道我在问一个类似的问题,这个问题已经有很多答案了。但我无法找到解决我问题的办法。 这里我使用的是Laravel5.2,我试图从mysql数据库中获取latlng并显示在地图中。我正在使用谷歌地图api v2。我的确切问题是,我在控制台中获取latlng值,但我无法在地图上显示它。这里我附上完整的代码。任何帮助都将是巨大的

这是我的MapController.php

class MapController extends Controller
{
public $type = 'Map';
public function getIndex()
{

   $location = DB::table('eventdata')->simplePaginate(500);
    return view('map.map')->with('location', $location);
}
这是我的map.blade.php

    <head>
    <meta charset="utf-8">
    <script async defer
            src="https://maps.googleapis.com/maps/api/js?key=AIzaSyByrgAtWpei65izUKYhdyr9-r54rrMZ8Zc&callback=initialize">
    </script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="{{ asset('ico/opengts.png') }}">
    <title>Open GTS</title>

    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>

    <div class="navbar-wrapper">
        <div class="container">

            <div class="navbar navbar-inverse navbar-static-top" role="navigation">
                <div class="container">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand active" href="#">Geo Punch Solution</a>
                    </div>
                    <div class="navbar-collapse collapse">
                        <ul class="nav navbar-nav">
                            <li class=""><a href="{{ url('/') }}">Home</a></li>
                            <li class=""><a href="{{ url('aboutUs') }}">About Us</a></li>
                            <li class=""><a href="{{ url('accountAdmin') }}">Account Admin</a></li>
                            <li><a href="{{ url('userAdmin') }}">User Admin </a></li>

                            <li><a href="{{ url('vehicleAdmin') }}">Vehicle Admin </a></li>
                            <li><a href="{{ url('groupAdmin') }}">Group Admin </a></li>

                            <li><a href="{{ url('geozoneAdmin') }}">Geozone Admin </a></li>
                            <li><a href="{{ url('changePassword') }}">Change Password </a></li>
                        </ul>
                    </div>
                </div>
            </div>

            <script>
                function initialize() {

                    var map = document.getElementById('map');

                    // Initialise the map
                    var map_options = {
                        center: location,
                        zoom: 10,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    }
                    var maps = new google.maps.Map(map, map_options);

                    // Put all locations into array
                    var locations = [
                        @foreach ($location as $locations)
                        [ {{ $locations->latitude }}, {{ $locations->longitude }} ]
                        @endforeach
                        ];


                    for (i = 0; i < locations.length; i++) {
                        var location = new google.maps.LatLng(locations[i][0], locations[i][1]);

                        var marker = new google.maps.Marker({
                            position: location,
                            map: maps
                        });
                    }

                    // marker.setMap(map); // Probably not necessary since you set the map above

                }


                function handleLocationError(browserHasGeolocation, infoWindow, pos) {
                    infoWindow.setPosition(pos);
                    infoWindow.setContent(browserHasGeolocation ?
                            'Error: The Geolocation service failed.' :
                            'Error: Your browser doesn\'t support geolocation.');
                }
                {{--@endforeach--}}

            </script>

            <style type="text/css">
                #map {
                    width: 1170px;
                    height: 570px;
                    margin-top: 100px;
                    margin-left: 100px;
                }
            </style>
        </div>
    </div>
</head>
<body>

<div id="map"></div>
<p id="error"></p>
</body>

开放式GTS
切换导航
函数初始化(){ var map=document.getElementById('map'); //初始化地图 变量映射_选项={ 中心:位置, 缩放:10, mapTypeId:google.maps.mapTypeId.ROADMAP } var-maps=新的google.maps.Map(Map,Map\u选项); //将所有位置放入数组 变量位置=[ @foreach($location作为$locations) [{{$locations->latitude},{{{$locations->longitude}] @endforeach ]; 对于(i=0;i

我得到的错误如下

未捕获的TypeError:无法读取未定义的属性“77.597639984451”

请帮帮我。。 提前谢谢


注意:所有大括号和标记都已正确闭合。

正如Matej已经指出的,数组元素之间缺少逗号

var locations = [
    @foreach ($location as $locations)
    [ {{ $locations->latitude }}, {{ $locations->longitude }} ]
    @endforeach
];
这给了你这个JS,它是无效的:

var位置=[
[ 12.995250017848, 77.601860021241 ]
[ 13.003339979332, 77.597639984451 ]
[ 13.003339979332, 77.597639984451 ] 
];
您的代码应该是:

var locations = [
    @foreach ($location as $locations)
    [ {{ $locations->latitude }}, {{ $locations->longitude }} ],
    @endforeach
];
然后会给你这个JS:

var位置=[
[ 12.995250017848, 77.601860021241 ],
[ 13.003339979332, 77.597639984451 ],
[ 13.003339979332, 77.597639984451 ],
];
(最后一个元素上的尾随逗号不是必需的,可能会被任何JS代码linting工具标记为无效,但否则不会导致任何问题)

更新:这里有一个简单的方法,至少可以使用您现有的位置为地图本身提供某种中心。有更好的方法,例如使地图的边界适合所有位置,但您没有要求这样做

首先,创建位置数组。然后使用第一个设置地图的中心:

var locations = [
    @foreach ($location as $locations)
    [ {{ $locations->latitude }}, {{ $locations->longitude }} ],
    @endforeach
];

// Initialise the map
var map_options = {
    center: {lat: locations[0][0], lng: locations[0][1]},
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
var maps = new google.maps.Map(map, map_options);

位置
数组生成的JS是什么样子的?您可以使用以下命令设置地图中心:
中心:位置
。。。
location
定义在哪里?它看起来像什么?另外
locations
数组元素不被
分隔,
@duncan my js的latlng值如下[12.995250017848,77.601860021241][13.003339979332,77.597639984451][13.003339979332,77.597639984451]在第三行我得到了错误..@duncan实际上它不是中心:位置。它的中心:位置;非常感谢你。我可以看到u所说的输出,但看不到地图和标记。我在问题中粘贴了完整的代码。你能告诉我哪里出错了吗?@RahulVp在我看来,这个问题就像你在地图选项中指定地图的中心:
center:location,
locationdefined在哪里?var locations=[@foreach($location as$locations)[{{{$locations->latitude},{$locations->longitude}}],@endforeach]//初始化map var map_options={center:locations,zoom:10,mapTypeId:google.maps.mapTypeId.ROADMAP}var maps=new google.maps.map(map,map_options);对于(i=0;i