【问题标题】:Geolocation and php and database地理位置和 php 和数据库
【发布时间】:2012-05-10 20:38:20
【问题描述】:

我想在我的 phpwebsite 中使用地理位置。那我做了什么。用户可以将姓名、街道、房屋号码等信息添加到数据库中。这些信息清楚地存储在数据库中。如果人们使用搜索功能寻找地点,他们会收到他们搜索过的地点列表。 在列表中的每个已创建地点上,用户都可以单击 en 他们会收到有关特定地点(记录)的更详细的页面。在此页面中必须有地理位置。我已经使用谷歌,我发现我需要将地址转换为纬度/经度。必须将此纬度和经度添加到数据库中,但我不知道在什么时候(已经在添加函数中,在单独的函数之后)以及我将如何执行此操作。此时我有一个计算经度和纬度的函数。我将它们存储到 2 个变量中,在 javascript 中,但现在我被卡住了。 在这里你可以看到我已经拥有的代码。

<script>
$(document).ready(function () {
 
    // wire up button click
    $('#go').click(function () {
        // get the address the user entered
        var straat = $('#straat').val();
        var nummer = $('#nummer').val();
        var address = straat + " " + nummer;
        alert(address);
        if (address) {
            // use Google Maps API to geocode the address
            // set up the Geocoder object
            var geocoder = new google.maps.Geocoder();
            // return the coordinates
            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[0]) {
                        // print results
                        printLatLong(results[0].geometry.location.lat(),
                           results[0].geometry.location.lng());
                    } else {
                        error('Google did not return any results.');
                    }
 
                } else {
                    error("Reverse Geocoding failed due to: " + status);
                }
            });
        }
        else {
            error('Please enter an address');
        }
    });
 
});
 
// output lat and long
function printLatLong(lat, long) {
    var latitude = lat;
    var longitude = long;
    $('.results').text(latitude);
}
 
function error(msg) {
    alert(msg);
}
</script>

【问题讨论】:

    标签: php geolocation mysqli


    【解决方案1】:

    您将这些其他字段保存到数据库的什么位置?为什么不在同一点进行地理编码和存储纬度和经度?

    【讨论】:

    • 这就是我想做的,但我不知道怎么做。我有一个完整的 php 脚本来存储我的所有信息,但不是 LONG/LAT。我想同时存储它,但是如何存储。
    • 啊,我明白了,您可以使用 PHP 进行地理编码(谷歌有一个休息 API,您可以使用 file_get_contents 或 cURL)如果您在原始保存中执行此操作,您将拥有这些数据点
    • 好吧,看看你是否明白一切。由于谷歌的其余 api,我可以在不使用 javascript 的情况下使用地理编码。我只需要使用 file_get_contest 或 cURL?
    • 正确。它会给你返回 JSON,你可以对其进行 json_decode,这样你就可以使用它来获得你需要的部分
    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    相关资源
    最近更新 更多