从地址获取谷歌地图地理编码

Niv*_*Jah 3 php json google-maps

我正在尝试使用Google地图JSON请求从地址获取经度和经度,并在页面上显示地图.

从我的请求创建的URL工作正常,并使用JSON信息创建有效的URL:

http://maps.google.com/maps/api/geocode/json?address=<?php echo $_GET['q'];?>&sensor=false
Run Code Online (Sandbox Code Playgroud)

但是我的页面上一直出现错误:

ReferenceError:未定义file_get_contents

并没有显示任何内容.这是完整的HTML和JS代码:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places&language=iw"></script>
<script>
$geocode=file_get_contents("http://maps.google.com/maps/api/geocode/json?address=<?php echo     $_GET['q'];?>&sensor=false");

$output= json_decode($geocode);

$lat = $output.results[0].geometry.location.lat();
$lng = $output.results[0].geometry.location.lng();

var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(32.069373, 32.069373), // numbers are here for testing
mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
  mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map-canvas"></div>
Run Code Online (Sandbox Code Playgroud)

请注意,这allow_url_fopen = On是在我的php.ini中.

Niv*_*Jah 11

解决了,问题是脚本标签内的PHP代码.我将第一部分更改为:

<?php 
$geocode=file_get_contents("http://maps.google.com/maps/api/geocode/json?address=".$_GET['q']."&sensor=false");

$output= json_decode($geocode);

$lat = $output->results[0]->geometry->location->lat;
$lng = $output->results[0]->geometry->location->lng;
?>
Run Code Online (Sandbox Code Playgroud)