2016-09-26 124 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= 
相關問題