简体   繁体   中英

Multiple Address from a database in Google Maps API

I have a bunch of addresses in a database and I am trying to figure out how to put multiple addresses in the map. But the addresses is dependent on what the user searches such as city or number of bedrooms so its always changes depending on what they search. So far I have manage to have one address on a map. Any way I can combine the two so show the addresses you searched up along with its points on the map or modify the code that I already have?

here is the Google maps api code

            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);

here is the code searching up from the database

<?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>';
}?>

It would be nice to see the important part of the html. Is this a dynamic page where user add listings in the database?

The problem I found (in a similiar project I have) is that when you use "and", it seems that the user needs to check all boxes (or select options, etc). Nothing can be left alone, otherwise it creates a false search. You do get results from the query, but it may not be the right result. I have discovered that people use jquery, collecting boxes - checked, and comparing it to database. User may check all boxes, but most of the time, boxes will be left unchecked, and that means ALL in that category is good, not none. For instance, they don't check 2 or 3 or 4 baths. It means ALL properties are good as far as bath is concerned.

Instead of ...

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

Try:

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

Note you should also move the map.setCenter out of the codeAddress fn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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