Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Menambahkan beberapa penanda di Google Maps dari database MySQL

Akan sangat mudah untuk mengambil semua lat dan long.

require_once "db/db_handle.php";
$select = "SELECT * FROM map";
$data = $db->query($select);
foreach ($data as $key)
            $locations[]=array( 'name'=>'Location Name', 'lat'=>$key['lat'], 'lng'=>$key['lng'] );
}
/* Convert data to json */
$markers = json_encode( $locations ); 

Kemudian atur penanda peta google menggunakan penanda variabel php.

<script type='text/javascript'>
        <?php
            echo "var markers=$markers;\n";

        ?>
     function initMap() {

            var latlng = new google.maps.LatLng(-33.92, 151.25); // default location
            var myOptions = {
                zoom: 10,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: false
            };

            var map = new google.maps.Map(document.getElementById('map'),myOptions);
            var infowindow = new google.maps.InfoWindow(), marker, lat, lng;
            var json=JSON.parse( markers );

            for( var o in json ){

                lat = json[ o ].lat;
                lng=json[ o ].lng;
                name=json[ o ].name;

                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(lat,lng),
                    name:name,
                    map: map
                }); 
                google.maps.event.addListener( marker, 'click', function(e){
                    infowindow.setContent( this.name );
                    infowindow.open( map, this );
                }.bind( marker ) );
            }
        }

Kode keseluruhan akan menjadi :

 require_once "db/db_handle.php";
    $select = "SELECT * FROM map";
    $data = $db->query($select);
    foreach ($data as $key)
                $locations[]=array( 'name'=>'Location Name', 'lat'=>$key['lat'], 'lng'=>$key['lng'] );
    }
    /* Convert data to json */
    $markers = json_encode( $locations );
    ?>
    <!DOCTYPE html>
    <html>
      <head>
        <style>
          #map {
            height: 400px;
            width: 100%;
           }
        </style>
      </head>
      <body>
        <h3>My Google Maps Demo</h3>
        <div id="map"></div>
        <script type='text/javascript'>
        <?php
            echo "var markers=$markers;\n";

        ?>

        function initMap() {

            var latlng = new google.maps.LatLng(-33.92, 151.25); // default location
            var myOptions = {
                zoom: 10,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: false
            };

            var map = new google.maps.Map(document.getElementById('map'),myOptions);
            var infowindow = new google.maps.InfoWindow(), marker, lat, lng;
            var json=JSON.parse( markers );

            for( var o in json ){

                lat = json[ o ].lat;
                lng=json[ o ].lng;
                name=json[ o ].name;

                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(lat,lng),
                    name:name,
                    map: map
                }); 
                google.maps.event.addListener( marker, 'click', function(e){
                    infowindow.setContent( this.name );
                    infowindow.open( map, this );
                }.bind( marker ) );
            }
        }
        </script>
        <script async defer
        src="https://maps.googleapis.com/maps/api/js?key=myKey&callback=initMap">
        </script>
      </body>
    </html>



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. gunakan kumpulan hasil prosedur tersimpan mysql di prosedur tersimpan lainnya

  2. mysql_connect():Tidak ada file atau direktori seperti itu

  3. MySQL memesan berdasarkan bidang di Eloquent

  4. Bagaimana membandingkan dua daftar string yang dipisahkan koma menggunakan MySQL

  5. Bagaimana cara mengekspor struktur MySQL db ke file Excel?