Add dynamically markers from php controller

Viewed 719

I'm using google map API and would like to create markers dynamically. I'm using Zend Framework and this is a part of my code.

Who can help me fetch this object to get markers and then show them on a map ?

<script>

      function initMap() {
          
        var locations[];
        var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
        var map = new google.maps.Map(document.getElementById('ressearch'), {
          zoom: 15,
          center: {lat: 36.8454481, lng: 10.1995665}
        });
        
        var markers = <?php echo json_encode( $all_users_loc ); ?>;
        //console.info(markers);
        // Create an array of alphabetical characters used to label the markers.
        var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        for(var i=0;i<2;i++){
           location[i]=markers
         }
        var locations = [
        {lat: <?=$this->translate($all_users_loc[0][co_latitude])?>, lng: <?=$this->translate($all_users_loc[0][co_longitude])?>},
        {lat: 36.84573142925444, lng: 10.19872965563718},
        {lat: 36.84630667456557, lng: 10.199920556439793}
        ]
        // Add some markers to the map.
        // Note: The code uses the JavaScript Array.prototype.map() method to
        // create an array of markers based on a given "locations" array.
        // The map() method here has nothing to do with the Google Maps API.
        var markers = locations.map(function(location, i) {
          return new google.maps.Marker({
            position: location,
            label: labels[i % labels.length],
            icon: image,
            animation: google.maps.Animation.DROP,
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      google.maps.event.addDomListener(window, "load", initMap);

    </script>
<?php echo json_encode( $all_users_loc ); ?>
<?=$this->translate($all_users_loc[0][co_latitude])?>
<div id="ressearch"></div>


//this is the result 
[{"co_adresse":"adresse 1","co_longitude":"10.19959870","co_latitude":"36.84540516"},{"co_adresse":"adresse","co_longitude":"33.58555244","co_latitude":"10.33333330"}] 36.84540516

1 Answers
Related