2016-09-26 123 views
1

我上传了正在处理的Web应用程序到远程服务器。 除了地理定位,一切都很好。我不明白是什么问题,因为我没有收到任何错误信息。 这是涉及地理定位的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文件中获取隐藏输入的值。

+0

什么在返回的状态字段? 'echo $ status' – nogad

+0

我刚刚添加了这些代码行,它开始工作......我不知道为什么xD 'navigator.geolocation.getCurrentPosition(getUserCoordinates,geoError); (error){ console.log(error.code); }' 但我想明白..你能解释我吗? – seb

+0

对我没有多大意义。它可能只是暂时不可用。 – nogad

回答

0

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 { 

同时检查您是否未在浏览器中为远程域禁用地理位置。添加一些控制台日志记录,看看是什么,如果你的网站在HTTPS运行不代码

+0

好的,谢谢..我打开了铬控制台,它给了我这个错误消息:'getCurrentPosition()和watchPosition()不再对不安全的起源工作。要使用此功能,您应该考虑将应用程序切换到安全的来源,例如HTTPS。请参阅[链接](https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins)了解更多详情。' 所以我认为我发现问题... II'll使这个开关,我会更新你.. – seb

0

内运行,还需要改变这种

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

这个

https://maps.googleapis.com/maps/api/geocode/json?latlng= 
相关问题