【问题标题】:Geolocation is working in local but doesn't on remote server地理位置在本地工作,但不在远程服务器上
【发布时间】:2016-09-26 20:30:37
【问题描述】:

我将正在开发的网络应用上传到远程服务器。 一切都很好,除了地理位置。我不明白是什么问题,因为我没有收到任何错误消息。 这是涉及地理位置的 HTML 代码。

<tr>
<td>Ti trovi qui: <span id="location"></span></td>
</tr>
</table>
<input type="hidden" id="latitude" name="latitude">
<input type="hidden" id="longitude" name="longitude">
</form>

这些是我使用的脚本:(我对 javascript 和 jQuery 的准备并不充分)

function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
jQuery.ajax({
    type:'POST',
    url:'getLocation.php',
    data:'latitude='+latitude+'&longitude='+longitude,
    success:function(msg){
        if(msg){
            jQuery("#location").html(msg);
        }else{
            jQuery("#location").html('Not Available');
        }
    }
});
}

function getUserCoordinates(position){

var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById('latitude').value=latitude
document.getElementById('longitude').value=longitude

}

jQuery(document).ready(function(){
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(getUserCoordinates);
    navigator.geolocation.getCurrentPosition(showLocation);
} else { 
    jQuery('#location').html('Geolocation is not supported by this    browser.');
}
});

这是 getLocation.php 文件(但我不认为这是问题所在):

<?php

require_once 'metodi.php';

sec_session_start();

if(login_check() == true){
if(!empty($_POST['latitude']) && !empty($_POST['longitude'])){

    $_SESSION['latitude'] = $_POST['latitude'];
    $_SESSION['longitude'] = $_POST['longitude'];
    //Send request and receive json data by latitude and longitude
    $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($_POST['latitude']).','.trim($_POST['longitude']).'&sensor=false';
    $json = @file_get_contents($url);
    $data = json_decode($json);
    $status = $data->status;
    if($status=="OK"){
        //Get address from json data
        $location = $data->results[0]->formatted_address;
    }else{
        $location =  '';
    }
    //Print address
    echo $location;
}
} else {
    echo "Non sei autorizzato a visualizzare questa pagina. Effettua il login.";
}

?>

然后我在另一个 php 文件中获取隐藏输入的值。

【问题讨论】:

  • 返回的状态字段中有什么内容吗? echo $status
  • 我刚刚添加了这几行代码,它就开始工作了...我不知道为什么 xD navigator.geolocation.getCurrentPosition(getUserCoordinates, geoError); function geoError(error) { console.log(error.code); } 但我想了解...你能解释一下吗??跨度>
  • 对我来说没有多大意义。它可能只是暂时不可用。
  • 我同意 xD 我要疯了
  • 在共享主机上 IP 限制可能是一个因素

标签: javascript php jquery mysql geolocation


【解决方案1】:

如果你的网站是在https中运行的,你也需要改变这个

http://maps.googleapis.com/maps/api/geocode/json?latlng=

到这里

https://maps.googleapis.com/maps/api/geocode/json?latlng=

【讨论】:

    【解决方案2】:

    getCurrentPosition() 是异步的。当您调用它两次时,不能保证第一次调用会在第二次调用之前完成。背靠背调用两次也没有意义

    它还依赖第三方服务来返回数据

    只调用一次,然后在一个回调中将响应值传递给你的两个函数

    改变

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(getUserCoordinates);
        navigator.geolocation.getCurrentPosition(showLocation);
    } else { 
    

    到

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position){
            getUserCoordinates(position);
            showLocation(position);
       });
    
    } else { 
    

    还要检查您是否没有在浏览器中为远程域禁用地理定位。添加一些控制台日志记录以查看代码中正在运行和未运行的内容

    【讨论】:

    • 好的,谢谢……我打开了 chrome 控制台,它给了我这个错误消息:getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See [link](https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins) for more details. 所以我想我找到了问题……我会做出这个切换,我会更新你..
    猜你喜欢
    • 2018-11-30
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多