0

我在数据库中有一堆地址,我试图弄清楚如何在地图中放置多个地址。但是地址取决于用户搜索的内容,例如城市或卧室数量,因此它总是根据他们搜索的内容而变化。到目前为止,我已经设法在地图上拥有一个地址。有什么办法可以将两者结合起来,以便在地图上显示您搜索的地址及其点或修改我已经拥有的代码?

这是谷歌地图 api 代码

            var geocoder;
            var map;
            function initialize() {
              geocoder = new google.maps.Geocoder();
              var latlng = new google.maps.LatLng(49.2505, -123.1119);
              var mapOptions = {
                zoom: 15,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              }
              map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
            }

            function codeAddress() {
              var address = '<?php echo json_encode($varStreetAddress);?> <?php echo json_encode($varCity);?>, BC';
              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);

这是从数据库中搜索的代码

<?php
    $mysqli = new mysqli("localhost","root","", "");
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        }
///////////set search variables
$property = $_POST['property'];
$bedroom = $_POST['BedroomNumber'];
$bathroom = $_POST['BathroomNumber'];
$priceMin = $_POST['PriceMin'];
$priceMax = $_POST['PriceMax'];
$termlease = $_POST['TermLease'];
//////////search
if(isset($_POST['utilities']) && is_array($_POST['utilities'])) {
    foreach($_POST['utilities'] as $check) {
             //echoes the value set in the HTML form for each checked checkbox.
                         //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                         //in your case, it would echo whatever $row['Report ID'] is equivalent to.
    }
}


$sql = $mysqli->query("select * from propertyinfo where Property like '%$property%' and NumBed like '%$bedroom%' and NumBath like '%$bathroom%' and Price >= '$priceMin' and Price <= '$priceMax' and utilities like '%$check%' and TermLease like '%$termlease%'");


if($sql === FALSE) {
    die(mysql_error()); // TODO: better error handling

}

if($sql->num_rows){
    while ($row = $sql->fetch_array(MYSQLI_ASSOC)){
        echo '<div id="listing">
                    <div id="propertyImage"> 
                        <img src="uploadimages/'.$row['imageName1'].'" width="200" height="150" alt=""/> 
                    </div>

                    <div id="basicInfo">
                    <h2>$'.$row['Price'].'</h2>
                    <p style="font-size: 18px;"># '.$row['StreetAddress'].', '.$row['City'].', BC</p>
                    <p>'.$row['NumBed'].' Bedrooms | '.$row['NumBath'].' Bathrooms | '.$row['Property'].'</p>
                    <br>
                    <p><a href="output2.php?record_id='.$row['ID'].'" class="link2" target="_blank">View Full Details</a> | <a href="" class="link2">Get Directions</a>

                    </div>
                </div>';

    }
}
else
{
echo '<h2>0 Search Results</h2>';
}?>
4

2 回答 2

0

代替 ...

function codeAddress() {
   var address = '<?php echo json_encode($varStreetAddress);?> <?php echo json_encode($varCity);?>, BC';
   geocoder.geocode( ....
}

尝试:

function codeAddress(address) {
   geocoder.geocode( ....
}
<?php
   while ($row = $sql->fetch_array(MYSQLI_ASSOC)){
      ...
      print "codeAddress($jsonAddress);\n";
      ...

请注意,您还应该将 map.setCenter 移出 codeAddress fn

于 2013-07-02T12:31:41.297 回答
0

很高兴看到 html 的重要部分。这是用户在数据库中添加列表的动态页面吗?

我发现的问题(在我的一个类似项目中)是当您使用“and”时,用户似乎需要选中所有框(或选择选项等)。什么都不能独处,否则会造成错误的搜索。您确实从查询中获得了结果,但它可能不是正确的结果。我发现人们使用 jquery,收集框 - 选中,并将其与数据库进行比较。用户可以选中所有框,但大多数情况下,框不会被选中,这意味着该类别中的 ALL 是好的,而不是没有。例如,他们不检查 2 或 3 或 4 个浴室。这意味着就浴缸而言,所有属性都很好。

于 2013-07-02T11:37:20.593 回答