David February 2016

Google maps multiple markers with lng and lat given

I need to display muiltiple markers on my home page on a map. I;am storing each Travel Flyers with a lng and lat in the database, then I display it for each flyer. (Which works fine), but when I display it on my home page with this code, it only displays the last lat and lng that was inserted into the database.

Here is my code:

 <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBaB_9hgeARViVKIT6O1pFKXRCSuYaol2A&libraries=places&callback=initAutocomplete"></script>
    <script>


            jQuery(function($) {
                // Asynchronously Load the map API
                var script = document.createElement('script');
                script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
                document.body.appendChild(script);
            });

            function initialize() {

            @foreach($flyer as $flyers)
                var lat = {{ $flyers->lat }};
                var lng = {{ $flyers->lng }};
            @endforeach

                var map;
                var bounds = new google.maps.LatLngBounds();
                var mapOptions = {
                    mapTypeId: 'roadmap'
                };

                // Display a map on the page
                map = new google.maps.Map(document.getElementById("map"), mapOptions);
                map.setTilt(45);


                // Multiple Markers
                var markers = [
                    [lat, lng ]
                ];




                // Display multiple markers on a map
                var infoWindow = new google.maps.InfoWindow(), marker, i;

                // Loop through our array of markers & place each one on the map
                for( i = 0; i < markers.length; i++ ) {
                    var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
                    bounds.extend(position)        

Answers


David February 2016

Got it! This is what I had to do:

// Multiple Markers
var markers = [
@foreach($flyer as $flyers)
        ['', {{ $flyers->lat }}, {{ $flyers->lng }} ],
@endforeach
];

Post Status

Asked in February 2016
Viewed 1,795 times
Voted 11
Answered 1 times

Search




Leave an answer