javascript - 从 php Controller 动态添加标记

标签 javascript arrays json google-maps google-maps-api-3

我正在使用谷歌地图 API,并且想动态创建标记。我正在使用 Zend Framework,这是我代码的一部分。

谁可以帮我获取这个对象以获取标记,然后将它们显示在 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

最佳答案

试试这个:

<?php 

    $json = '[{
      "co_adresse": "adresse 1",
      "co_longitude": "10.19959870",
      "co_latitude": "36.84540516"
    }, {
      "co_adresse": "adresse",
      "co_longitude": "33.58555244",
      "co_latitude": "10.33333330"
    }]';
     $all_users_loc = json_decode($json, true);


     $arrary = [];
     foreach($all_users_loc as $key) {
      $arrary[] =  array(($key['co_latitude']*1), ($key['co_longitude']*1));


    }
?>




<script>


function markersOnMap() {

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

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

    map.setTilt(50);

    // Multiple markers location, latitude, and longitude
   var markers = <?php echo json_encode($array) ;?>;

    /*       var markers = [
        [40.671531, -73.963588],
        [40.672587, -73.968146],
        [40.665588, -73.965336]
    ];   
      */
    // Info window content
    var infoWindowContent = [
        ['<div class="info_content">' +
        '<h3>Brooklyn Museum</h3>' +
        '<p>The Brooklyn Museum is an art museum located in the New York City borough of Brooklyn.</p>' + '</div>'],
        ['<div class="info_content">' +
        '<h3>Brooklyn Public Library</h3>' +
        '<p>The Brooklyn Public Library (BPL) is the public library system of the borough of Brooklyn, in New York City.</p>' +
        '</div>'],
        ['<div class="info_content">' +
        '<h3>Prospect Park Zoo</h3>' +
        '<p>The Prospect Park Zoo is a 12-acre (4.9 ha) zoo located off Flatbush Avenue on the eastern side of Prospect Park, Brooklyn, New York City.</p>' +
        '</div>']
    ];

    // Add multiple markers to map
    var infoWindow = new google.maps.InfoWindow(), marker, i;

    // Place each marker on the map  
    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][0], markers[i][1]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0]
        });

        // Add info window to marker    
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infoWindow.setContent(infoWindowContent[i][0]);
                infoWindow.open(map, marker);
            }
        })(marker, i));

        // Center the map to fit all markers on the screen
        map.fitBounds(bounds);
    }

    // Set zoom level
    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
        this.setZoom(14);
        google.maps.event.removeListener(boundsListener);
    });

}

// Load initialize function
google.maps.event.addDomListener(window, 'load', markersOnMap);    
</script>

关于javascript - 从 php Controller 动态添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45437255/

相关文章:

php - Laravel+Dingo 中一致的 REST API 响应

javascript - 语义 UI 下拉菜单需要点击 2 次才能触发

javascript - 有没有办法像我一样对两个并行数组进行排序

python - numpy 数组乘法比带向量乘法的循环慢?

c - 从多个 sockaddr_storage 中提取 IP 和端口到 char* 中

javascript - 加载外部数据并保持全局 javascript

ios - 如何编码 UIImage 以进行 JSON 传输?

javascript - Moment.js 以本地格式显示小时

javascript - Webpack 导入 css 时出现错误

javascript - 更改 react 大日历中的行样式