2013-07-10 57 views
2

我试图从一个谷歌地图JSON请求的地址获取经度和纬度,并在页面上显示地图。谷歌地图地址代码从地址

正从我的要求创建的URL工作正常,并创建JSON信息的有效网址:

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

但我不断收到我的网页上的错误:

ReferenceError: file_get_contents is not defined

而且什么都没有显示。下面是完整的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> 

注意allow_url_fopen = On是我的php.ini。

+0

的我想你是用PHP函数在JavaScript部分 – Sutulustus

+0

告诉我们生成的URL看起来像 – gawi

+0

请发表您的解决方案作为一个答案,并接受它。 – geocodezip

回答

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; 
?>