0

我有 5 个字段的 HTML 表单。

1) 地址 2) 城市 3) 州 4) 国家 5) 邮政编码。

输入此字段值后,它会显示谷歌地图。

谷歌地图代码:

<?php
$add = urlencode($_POST['address']);
$city = urlencode($_POST['city']);
$state = urlencode($_POST['state']);
$country  = urlencode($_POST['country']);
$zip = $_POST['zip'];

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode   
/json?address='.$add.',+'.$city.',+'.$state.',+'.$country.'&sensor=false');

$output= json_decode($geocode); //Store values in variable

if($output->status == 'OK'){ // Check if address is available or not
echo "<br/>";
echo "Latitude : ".$lat = $output->results[0]->geometry->location->lat; //Returns Latitude
echo "<br/>";
echo "Longitude : ".$long = $output->results[0]->geometry->location->lng; // Returns Longitude
?>
<script type="text/javascript">
$(document).ready(function () {
// Define the latitude and longitude positions
var latitude = parseFloat("<?php echo $lat; ?>"); // Latitude get from above variable
var longitude = parseFloat("<?php echo $long; ?>"); // Longitude from same
var latlngPos = new google.maps.LatLng(latitude, longitude);
// Set up options for the Google map
var myOptions = {
zoom: 10,
center: latlngPos,
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControlOptions: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE
}
};
// Define the map
map = new google.maps.Map(document.getElementById("map"), myOptions);
// Add the marker
var marker = new google.maps.Marker({
position: latlngPos,
map: map,
title: "test"
});
});
</script>
<div id="map" style="width:450px;height:350px; margin-top:10px;"></div> // Div in which 
Google   Map will show
<?php
}
?>

但是在进程页面中提交后显示以下错误:

警告:file_get_contents(http://maps.google.com/maps/api/geocode
/json?address=,+Dhaka,+,+Bangladesh&sensor=false):打开流失败:HTTP 请求失败!第 19 行 D:\Software\Installed\xampp\htdocs\Classified-website \lat-long.php 中的 HTTP/1.0 504 网关超时

注意:尝试在第 23 行的 D:\Software\Installed\xampp\htdocs\Classified- website\lat-long.php 中获取非对象的属性

我的代码有什么问题?有人帮我吗?
谢谢。

没有帮助的回答!

4

2 回答 2

2

From Wikipedia's article on HTTP status codes:

504 Gateway Timeout

The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.

So, either you have a proxy server sitting between you and the internet, and it returned this error, or the Google Maps server(s) you're hitting are non-responsive and their reverse proxies / load balancers can't serve you the page in time.

Speak to your web hosting provider about any proxy server they might be using. As it looks like this is on your local PC, then chances are that your "web hosting provider" is either your IT department (business, school) or your ISP (home user). If they aren't using a proxy, then the problem probably isn't on your end and isn't under your control.

于 2012-12-16T18:41:10.510 回答
0

尝试这个:

        $arrContextOptions=array(
            "ssl"=>array(
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ),
        ); 

        $geocode=file_get_contents(
            'http://maps.google.com/maps/api/geocode/json?address='.$add.',+'.$city.',+'.$state.',+'.$country.'&sensor=false', false, stream_context_create($arrContextOptions)
        );
于 2018-03-19T13:53:45.267 回答