本文介绍了将变量从javascript传递给php以获取地理定位功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿家伙我想要我从javascript代码获得的经度和纬度,将它传递给php变量$ lat和$ lng,这样我就可以获得$ city并在我的sql查询中使用它(查询不包括在内)。以下是我的代码。请帮助 < html> < head> < script> 函数getLocation(){ if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(showPosition); } else { document.write(此浏览器不支持地理定位。); } } function showPosition(position){ var latitude = position.coords.latitude; var longitude = position.coords.longitude); } < / script> < / head> < body onload =getLocation()>< / body> < / html> <?php 函数getaddress($ lat,$ lng) { $ url ='http://maps.googleapis.com/maps/ API /地理编码/ JSON经纬度= '修剪($ LNG)。 '和?修剪($ LAT)。';传感器=假'; $ json = @file_get_contents($ url); $ data = json_decode($ json); $ status = $ data->状态; if($ status ==OK) { return $ data-> results [0] - > formatted_address; } else { return false; } } $ lat = 33.568711799999996; //纬度 $ lng = 35.3800354; // longitude $ address = getaddress($ lat,$ lng); if($ address) { //分隔符可能是斜线或逗号 // $ date =04/30/1973; list($ address,$ street,$ city)= split('[, - ]',$ address); echo地址:$地址;街道:$ street;城市:$ city< br /> \\\; } else { echoNot found; } ?> 解决方案 script.php 比使用jquery get $。get(script .php,{lat:123,lon:321},function(data){ alert(data); //或者你可以在你的块中插入结果文本}); 然后用这个开始你的PHP脚本: if(isset($ _ GET ['lat'])&& isset($ _ GET ['lon'])) PS不要忘记包含jquery lib Hey guys I want the longitude and latitude I get from the javascript code to pass it to the php variables $lat and $lng so I can get the $city and use it in my sql query(query not included). Below is my code. Please help <html><head><script>function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { document.write("Geolocation is not supported by this browser."); }}function showPosition(position) { var latitude= position.coords.latitude; var longitude= position.coords.longitude); }</script></head><body onload="getLocation()"></body></html><?php 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; } } $lat= 33.568711799999996; //latitude $lng= 35.3800354; //longitude $address= getaddress($lat,$lng); if($address) { // Delimiters may be slash, or comma //$date = "04/30/1973"; list($address, $street, $city) = split('[,-]', $address); echo "Address: $address; Street: $street; City: $city <br />\n"; } else { echo "Not found"; }?> 解决方案 You should move your PHP script to another file for e.g. script.php than with jquery get$.get( "script.php", { lat: "123", lon: "321" }, function( data ) { alert( data ); // or you can insert result text to your block});And start your PHP script with this:if(isset($_GET['lat']) && isset($_GET['lon']))P.S. Don't forget to include jquery lib 这篇关于将变量从javascript传递给php以获取地理定位功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 05:24