谷歌地图多个标记问题在PHP中

时间:2018-03-13 05:30:16

标签: javascript php html google-maps

我正在研究核心php,我必须从数据库中制作谷歌地图多个标记我已经尝试但显示一些问题(警报)如:(地理编码由于以下原因未成功:ZERO_RESULTS),我必须通过值数组($ list)到javascript var data =;在我的代码下面,请检查它&帮帮我......

.then()

1 个答案:

答案 0 :(得分:0)

不是将对象直接传递到codeAddress函数中,而是在循环中传递每个单独的地址 - 但如果源数据中有很多项目,则很容易超出每日配额。

最初您定义$locations=array();,然后尝试填充看似未声明的数组$list - 可能您打算填充$locations

<?php
    include "db_connection.php";

    $locations = array();
    $query =  $conn->query('SELECT `pg_address` FROM `tbl_master_property` limit 10');
    while( $row=$query->fetch_assoc() ) {
        $locations[] = $row['pg_address'];
    }
?>
<html>
  <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Geocoding service</title>
        <style>
          html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
          }
        </style>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
        <script type="text/javascript">
            var geocoder;
            var map;
            var data = <?php echo json_encode( $locations ); ?>;

            function initialize(){
              geocoder = new google.maps.Geocoder();
              var latlng = new google.maps.LatLng(53.3496, -6.3263);
              var mapOptions = {
                zoom: 8,
                center: latlng
              }
              map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

              for( var n in data ){
                codeAddress.call( this, data[ n ] );
              }
            }

            function codeAddress(address) {
              geocoder.geocode( {address:address}, function(results, status) {
                if( status == google.maps.GeocoderStatus.OK ) {

                  map.setCenter( results[0].geometry.location );

                  var marker = new google.maps.Marker({
                      map: map,
                      position: results[0].geometry.location
                  });

                } else {
                  alert('Geocode was not successful for the following reason: ' + status);
               }
              });
            }
            google.maps.event.addDomListener( window, 'load', initialize );

        </script>
    </head>
    <body>
        <div id="map-canvas"></div>
    </body>
</html>