2011-12-17 76 views
2

我正在使用html5和java脚本创建一个简单的应用程序。 我想显示任何一个城市最近的地方 所以我已经设置了一个国家的长期和纬度。如何获得放置在Google地图上的JSON结果?

如果用户点击学校,我想显示所有学校名称与半径最接近的长和长。
如果用户点击教堂,我想显示所有教会名称与半径最接近的长和长。
如果用户点击医院,我想显示所有医院名称最接近半径为long和lat的医院名称。等

我只有这个链接和示例代码有关:

https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyC1BIAzM34uk6SLY40s-nmXMivPJDfWgTc 


$.getJSON("https://maps.googleapis.com/maps/api/place/search/json?",{ 
     location:"-33.8670522,151.1957362", 
     radius: 500, 
     types:'food', 
     name: 'harbour', 
     sensor: false, 
     key: 'AIzaSyC1BIAzM34uk6SLY40s-nmXMivPJDfWgTc' 
     format: "json" 
    }, 
    function(data) { 
     $.each(data.results, function(i,item){ 
     $("<img/>").attr("src", results.name).appendTo("#images"); 
     if (i == 3) return false; 
    }); 
    }); 

所以在这里我要改变得到地名。 最初我该怎么办?

+0

请参阅更新的答案。有错别字 – mplungjan 2011-12-22 09:37:45

回答

1

地图V3不支持回拨/ JSONP从一个jQuery获得/的getJSON此时

http://www.quora.com/Why-doesnt-the-Google-Maps-API-support-JSONP

那说 - 如果你有耐心尝试寻找

http://code.google.com/intl/no-NO/apis/maps/documentation/javascript/services.html#Geocoding

加载异步,你需要做这样的事情:

function loadScript() { 
    var script = document.createElement("script"); 
    script.type = "text/javascript"; 
    script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize"; 
    document.body.appendChild(script); 
} 

http://code.google.com/apis/maps/documentation/javascript/basics.html#Async


如果不是,这里是我的建议。

使用代理:

除非有人能找到新的资源,显示了如何用JSONP做到这一点(我只能找到资源告诉我,这是不支持)的方式就是写一个代理。

此作品来自同一服务器

DEMO

HTML:

<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 
    $.getJSON("googleapijson.php?type=food", 
    function(data, textStatus){ 
     $.each(data.results,function(i, item) {; 
     $("#placenames").append(i+':'+item.vicinity+'<br/>'); 
    }); 
    }); 
}); 
</script> 
</head> 
<body> 
<div id="placenames"></div> 
</body> 
</html> 

PHP:

<?PHP 

header("Content-type: application/json"); 

/** note you need to grab and clean the parameters from the 
* get/post request and build your querystring: 
*/ 

$type = $_GET["type"]; // this needs to be cleaned 

$URL="https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=".$type."&name=harbour&sensor=false&key=AIzaSyC1BIAzM34uk6SLY40s-nmXMivPJDfWgTc" 

$json = file_get_contents($URL); 

echo $json; 
?> 
+0

非常感谢。 – SANR1103219 2012-01-03 07:18:38

相关问题