使用Google地图查询查询的最高结果

时间:2014-11-07 00:55:05

标签: javascript php mysql google-maps google-maps-api-3

我在将一些东西放在一起时遇到了麻烦。我可以创建一个显示mysql数据库结果的php页面,我可以通过google获取地址,但我似乎无法将它们放在一起。

我要做的是映射数据库搜索的前2或3个结果。我似乎无法将结果中的地址传入地理编码器。我尝试了几种不同的方式,但我真的不知道从哪里开始,我正在阅读的很多内容都在我头上。 (Javascript不是我以前真正合作过的东西。)

我已经获得了以下代码。我知道它缺少一些元素因此不起作用。如果有人可以帮助我并向我展示如何正确传递我的价值观,那就太好了。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script>
var geocoder;
var map;
function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(39.174208, -84.481842);
    var mapOptions = {
        zoom: 10,
        center: latlng
    }
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}


function codeAddress() {
    //How do I get these addresses from the query?
    var address = document.getElementById('address').value;
    var address2 = document.getElementById("address2").value;

    geocoder.geocode( { 'address': address2}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        }
        else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });


    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        }
        else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
</script>
</head>
<body onload="initialize()">
<?php
//Include Function- positive this is fine.
include ('../includes/dbCon.php');
echo "Page Header Stuff<br><br>";
$query= "SELECT full_addr from address_lst limit 2";
$result = mysql_query($query, $con);
echo "<div>";
while ($row = mysql_fetch_assoc($result)) {
    echo $row['addr'];
}
?>
<input type="button" value="Map These!" onclick="codeAddress()">
</div>
<div id="map-canvas" style="width: 680px; height: 480px;"></div>
</body>
</html>

提前致谢!

1 个答案:

答案 0 :(得分:0)

您必须使用所需的ID(地址和地址2)和所需的值创建元素(例如输入&#39;)

$i=0;
while ($row = mysql_fetch_assoc($result)) {
    echo '<input id="address'.((!!$i++)? $i : '').'" 
                 value="'.htmlentities($row['addr']).'" />';
}