【问题标题】:Map top results from a query wth Google Maps使用 Google 地图映射查询的热门结果
【发布时间】:2015-01-03 17:08:39
【问题描述】:

我无法将一些东西放在一起。我可以制作一个 php 页面来显示来自 mysql 数据库的结果,并且我可以获取与 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>

提前致谢!

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我不能投票给你的答案,因为我太新了,但这正是这个新手所需要的。我以前从未使用过 htmlentities() ,甚至不知道谷歌这个。只要知道从哪里开始寻找就会有很大帮助! Danke Schön!
    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多