2017-05-10 2 views
0

먼저, 제 영어를 사과하십시오.곱슬 머리 사용자의 주소를 인쇄하는 방법

안녕하세요! 나는이 작은 스크립트가, 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"; 
    } 
?> 

을,하지만 난 현재에 $ 위도와 $ 긴 변수를 변경하는 방법을 모른다 사용자 위치.

몇 마디로; 이 스크립트가 작동하도록 PHP 변수에 현재 사용자의 위도/경도를 어떻게 전달할 수 있습니까?

+0

PHP에 의해이 기능을 제공하지 않습니다 사용하여 다른 스크립트입니다 기본값을 사용하려면 정보를 얻기 위해 일종의 외부 API를 사용해야합니다. – Enstage

+0

감사합니다. Google Maps API에서 검색을 시작합니다. –

+0

사용자가 IP 주소를 위장 할 수있는 프록시 설정이나 방화벽이있는 경우 아무도 정확한 주소 또는 위도 lang을 보장 할 수 없습니다. –

답변

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> 

이것은 자바 스크립트 기반 검색입니다. 당신은 HTML 브라우저에서 그것을 시도하고 php 스크립트에 긴 오랫동안 전달할 수 있습니다.

괜찮 으면 나에게 말해 줄 수 있다면 다른 방법도 있습니다.

1

자바 스크립트 또는 다른 google api를 사용해야합니다. 당신은 lat & lang을 별도의 숨겨진 필드에두고 숨겨진 필드에서 PHP 변수를 할당 할 수 있습니다. 여기

이는 HTML 5와 자바 스크립트를 사용하여 얻을 수있는 예제 스크립트입니다

<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> 
관련 문제