简体   繁体   English

将 mysql 和 php 与谷歌地图一起使用

[英]using mysql and php with google maps

I'm trying loop through every row in a table and set a marker using the value provided in the "address column".我正在尝试遍历表中的每一行,并使用“地址列”中提供的值设置标记。 The js code and the php code until setMarker() works fine but I'm not sure how to go about using the function inside php code. The js code and the php code until setMarker() works fine but I'm not sure how to go about using the function inside php code.

<?php
require 'private/database.php';

$sql = "SELECT * FROM form;";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<script>setMarker($row["address"]);</script>';
    }
} else {
    echo '<script>console.log("ERROR: marker database empty");</script>';
}
var map;
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
  zoom: 15,
  center: {lat: 24.149950, lng: 120.638610},
  mapId: '63d22d3ae6cf15ff'
  });
}


// geocoder
function setMarker(address) {
    const geocoder = new google.maps.Geocoder();
  geocoder.geocode({address: address, componentRestrictions: {
    country: 'TW'}}, (results, status) => {
    if (status === 'OK') {
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
    } else {
        alert(`Geocode unsuccessful for address: "${address}"\nSTATUS CODE: ${status}`);
    }
  });
}

This is more or less what I meant with the comment I made - this way the map is created by the initMap callback function before any attempts to create the markers is made - which might not be the case in the original.这或多或少是我对我所做的评论的意思——这样,在尝试创建标记之前,map 是由initMap回调 function 创建的——原始情况可能并非如此。 This is quickly put together and not tested.这很快就放在一起,没有经过测试。

<script>
    var map;
    <?php


        require 'private/database.php';

        $sql = "SELECT * FROM form;";
        $result = mysqli_query($conn, $sql);
        $data=array();


        while ($row = mysqli_fetch_assoc($result)) {
            $data[]=$row;
        }
        // create the js variable
        printf(
            'var json=%s;',
            json_encode( $data )
        );
    ?>

    
    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
          zoom: 15,
          center: {lat: 24.149950, lng: 120.638610},
          mapId: '63d22d3ae6cf15ff'
      });
      Object.keys(json).forEach( key => {
        let obj=json[ key ];
        setMarker( obj['address'] )
      })
    }



    function setMarker(address) {
        const geocoder = new google.maps.Geocoder();
        geocoder.geocode( { address: address, componentRestrictions: { country: 'TW'} }, (results, status) => {
            if (status === 'OK') {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert(`Geocode unsuccessful for address: "${address}"\nSTATUS CODE: ${status}`);
            }
        });
    }
    
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM