2017-05-10 37 views
0

首先,請道歉我的英文。如何打印curret用戶的地址

嗨!我需要用PHP打印當前user's地址,我有這個小腳本:

<? 
    function getaddress($lat,$lng) 
    { 
    $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false'; 
    $json = @file_get_contents($url); 
    $data=json_decode($json); 
    $status = $data->status; 
    if($status=="OK") 
    { 
     return $data->results[0]->formatted_address; 
    } 
    else 
    { 
     return false; 
    } 
    } 
?> 

<?php 
    $lat= 21.884766199999998; //latitude 
    $lng= -102.2996459; //longitude 
    $address= getaddress($lat,$lng); 
    if($address) 
    { 
    echo $address; 
    } 
    else 
    { 
    echo "Not found"; 
    } 
?> 
當然它工作的

,但我不知道如何改變$ lat和$長變量當前用戶位置。

幾句話;如何將當前用戶經緯度和長位置傳遞給PHP變量以讓此腳本起作用?

+0

PHP不通過提供這種功能的另一個腳本默認情況下,您需要使用某種外部API來獲取信息。 – Enstage

+0

感謝您的回答,我開始在Google Maps Api中搜索。 –

+0

如果用戶有一些代理設置或防火牆來欺騙IP地址,任何人都不能保證正確的地址或經緯度 –

回答

1
<html> 
<body> 
<p id="demo">Click the button to get your coordinates:</p> 
<button onclick="getLocation()">Try It</button> 
<script> 
var x=document.getElementById("demo"); 
function getLocation() 
    { 
    if (navigator.geolocation) 
    { 
    navigator.geolocation.getCurrentPosition(showPosition); 
    } 
    else{x.innerHTML="Geolocation is not supported by this browser.";} 
    } 
function showPosition(position) 
    { 
    x.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;  
    } 
</script> 
</body> 
</html> 

這是一個基於JavaScript的搜索。你可以在html瀏覽器中嘗試它,並將經緯度傳遞給php腳本。

如果它可以幫助你確定,或者你可以告訴我,我也有其他方法。

1

你需要使用JavaScript或其他谷歌API。你可以在一個單獨的隱藏字段中放置經緯度& lang,然後從該隱藏字段分配給你的php變量。

下面是一個示例腳本開始使用HTML 5和Java腳本這個

<script> 
var x = document.getElementById("demo"); 
function getLocation() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition); 
    } else { 
     x.innerHTML = "Geolocation is not supported by this browser."; 
    } 
} 
function showPosition(position) { 
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
} 
</script> 

這是使用谷歌的地理定位API

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Geolocation</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
     /* Always set the map height explicitly to define the size of the div 
     * element that contains the map. */ 
     #map { 
     height: 100%; 
     } 
     /* Optional: Makes the sample page fill the window. */ 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
    </style> 
    </head> 
    <body> 
    <div id="map"></div> 
    <script> 
     // Note: This example requires that you consent to location sharing when 
     // prompted by your browser. If you see the error "The Geolocation service 
     // failed.", it means you probably did not give permission for the browser to 
     // locate you. 
     var map, infoWindow; 
     function initMap() { 
     map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: -34.397, lng: 150.644}, 
      zoom: 6 
     }); 
     infoWindow = new google.maps.InfoWindow; 

     // Try HTML5 geolocation. 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
      var pos = { 
       lat: position.coords.latitude, 
       lng: position.coords.longitude 
      }; 

      infoWindow.setPosition(pos); 
      infoWindow.setContent('Location found.'); 
      infoWindow.open(map); 
      map.setCenter(pos); 
      }, function() { 
      handleLocationError(true, infoWindow, map.getCenter()); 
      }); 
     } else { 
      // Browser doesn't support Geolocation 
      handleLocationError(false, infoWindow, map.getCenter()); 
     } 
     } 

     function handleLocationError(browserHasGeolocation, infoWindow, pos) { 
     infoWindow.setPosition(pos); 
     infoWindow.setContent(browserHasGeolocation ? 
           'Error: The Geolocation service failed.' : 
           'Error: Your browser doesn\'t support geolocation.'); 
     infoWindow.open(map); 
     } 
    </script> 
    <script async defer 
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> 
    </script> 
    </body> 
</html>