2013-07-01 43 views
0

我在數據庫中有一堆地址,我試圖弄清楚如何在地圖中放置多個地址。但地址取決於用戶搜索的內容,例如城市或臥室數量,因此其取決於他們搜索的內容總是會發生變化。到目前爲止,我已經在地圖上設置了一個地址。任何方式,我可以結合這兩個所以顯示您搜索的地址,以及它在地圖上的點或修改我已經有的代碼?谷歌地圖API中的數據庫中的多個地址

這裏是谷歌地圖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>'; 
}?> 

回答

0

搜索了這將是很好看HTML的重要組成部分。這是一個用戶在數據庫中添加列表的動態頁面嗎?

我發現的問題(在一個類似的項目中)是,當您使用「和」時,似乎用戶需要檢查所有框(或選擇選項等)。沒有東西可以獨立存在,否則會造成錯誤的搜索。您確實從查詢中獲得結果,但這可能不是正確的結果。 我發現人們使用jQuery,收集箱子 - 檢查,並將其與數據庫進行比較。 用戶可以檢查所有的框,但大多數情況下,框將不被選中,這意味着該類別中的所有內容都很好,而不是沒有。例如,他們不檢查2或3或4浴。這意味着就沐浴而言,所有的性能都很好。

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