如何让放在谷歌地图的JSON结果? [英] how to get JSON Results placed on Google Maps?
问题描述
我创建使用HTML5和Java Script一个简单的应用程序。 我想表现出任何一个城市的最近的地方 所以我已经设置长,土地增值税的国家的。
如果用户点击学校,我想告诉所有的学校名称最接近的长和LAT半径。
如果用户单击教会我想说明最近的长和LAT半径所有教堂的名字。
如果用户点击医院我想说明最近的长和LAT半径所有的医院名称。等等。
我只有这个链接和样品相关的code:
<$p$p><$c$c>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?,{ 位置: - 33.8670522,151.1957362 半径:500, 类型:食物, 名称:港, 传感器:假的, 键:AIzaSyC1BIAzM34uk6SLY40s-nmXMivPJDfWgTc 格式为:JSON }, 功能(数据){ $每个(data.results,功能(I,项目){ $(&LT; IMG /&gt;中)。ATTR(src用户,results.name).appendTo(#图像); 如果(我== 3)返回false; }); });
所以,我想改变得到地名。 起初我该怎么办?
地图V3不支持回拨/ JSONP从一个jQuery获取/的getJSON此时
http://www.quora.com/为什么-犯规的非谷歌 - 地图-API的支持,JSONP
这是说 - 如果你有耐心尝试寻找
的http://$c$c.google.com/intl/no-NO/apis/maps/documentation/javascript/services.html#Geocoding
要异步加载,你需要做的是这样的:
函数loadScript(){
VAR脚本= document.createElement方法(脚本);
script.type =文/ JavaScript的;
script.src =http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize;
document.body.appendChild(脚本);
}
的http:// code。 google.com/apis/maps/documentation/javascript/basics.html#Async
如果不是,这里是我的建议。
使用代理:
除非有人能找到新的资源,显示如何使用JSONP做到这一点(我只能找到资源,告诉我不支持它)的方式是编写一个代理。
本作品来自同一服务器
HTML:
&LT; HTML&GT;
&LT; HEAD&GT;
&所述;脚本的src =https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js&GT;&所述; /脚本&GT;
&LT;脚本&GT;
$(文件)。就绪(函数(){
$ .getJSON(googleapijson.php?类型=食品,
功能(数据,textStatus){
$每个(data.results,功能(I,项目){;
$(#地名)追加(I +:+ item.vicinity +'&LT; BR /&GT;');
});
});
});
&LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
&LT; DIV ID =地名&GT;&LT; / DIV&GT;
&LT; /身体GT;
&LT; / HTML&GT;
PHP:
&LT; PHP
标题(内容类型:应用程序/ JSON);
/ **注意,你需要抓住和清理从参数
* GET / POST请求,并建立自己的查询字符串:
* /
$型= $ _GET [型]; //这需要清洁
$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($网址);
回声$ JSON;
?&GT;
I am creating a simple apps using html5 and java script. I want to show nearest places of any one city so already I set long and lat of a country.
If user click school I want to show all school names nearest of long and lat with radius.
If user click church I want to show all church names nearest of long and lat with radius.
If user click hospital I want to show all hospital names nearest of long and lat with radius. etc.
I only have this link and sample related code:
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;
});
});
so where I want to change to get place name. initially what do I do?
Maps v3 does not support callback/JSONP from a jQuery get/getJSON at this time
http://www.quora.com/Why-doesnt-the-Google-Maps-API-support-JSONP
That said - if you have the patience try looking at
http://code.google.com/intl/no-NO/apis/maps/documentation/javascript/services.html#Geocoding
To load async, you need to do something like this:
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
If not, here is what I suggest.
USING PROXY:
Unless someone can find a new resource that shows how to do this with jsonp (I could only find resources telling me it was not supported) a way is to write a proxy.
This works FROM THE SAME SERVER
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;
?>
这篇关于如何让放在谷歌地图的JSON结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!