javascript 从谷歌地图读取json文件放置api(使用javascript或php)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7050209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading the json file from google maps places api (with javascript or php)
提问by mithunsatheesh
I am building an application in which i am trying to fetch the list of places near to a given lat and long.
我正在构建一个应用程序,我试图在其中获取靠近给定纬度和经度的地点列表。
i got a api key and entering this on my browser url works and gives me a json file with the places data.
我得到了一个 api 密钥并在我的浏览器 url 上输入它,并为我提供了一个包含地点数据的 json 文件。
https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I
but when i try to fetch and parse it though j query its not giving me anything.
但是当我尝试获取和解析它时,虽然 j 查询它没有给我任何东西。
$(document).ready(function(){
$.getJSON('https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I',
function(data) {
alert(data);
});
i found the following questions on stack overflow but they dint reach me to an answer.
我发现了以下有关堆栈溢出的问题,但它们无法让我找到答案。
parse google place json with javascript
使用 javascript 解析 google place json
How do I use Google Places to get an array of Place names?
EDIT:
编辑:
i tried to get contents of the file using php by
我试图通过 php 获取文件的内容
echo file_get_contents("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I");
and got the file contents using php.
并使用php获取文件内容。
But is there a way to parse this with jquery or javascript ?
但是有没有办法用 jquery 或 javascript 解析这个?
Thank you for your help.
谢谢您的帮助。
采纳答案by VDP
It's not possible using ajax (directly) cross domain request are not allowed. I've tried:
不允许使用ajax(直接)跨域请求是不可能的。我试过了:
echo file_get_contents("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I");
it works for me (wamp 2.0 openssl enabled).
它对我有用(启用了 wamp 2.0 openssl)。
if you succeed in this, you can get the content using your ajax script on the page in the same domain.
如果你成功了,你可以在同一个域中的页面上使用你的 ajax 脚本获取内容。
EDIT:
编辑:
The idea is to do an ajax call to your php page, the php page gets data from de google api and returns it. jquery automatically parses json :)
这个想法是对你的php页面进行ajax调用,php页面从de google api获取数据并返回它。jquery 自动解析 json :)
in code:
在代码中:
$.ajax({
url: 'url_to_your_php',
data: {
location:'-33.8670522,151.1957362',
radius:500,
sensor:false,
key:'AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I'},
dataType: "json"
type: "GET", //or POST if you want => update your php in that case
success: function( data ) {
for(var i in data.results){
doSomthingWithYourLocation(data.results[i]);
}
},
error: function (request, status, error) {
//handle errors
}
});
Your php shoud do somthing like this:
你的 php 应该做这样的事情:
<?php
header('Content-type: application/json');
echo file_get_contents("https://maps.googleapis.com/maps/api/place/search/json?" .
"location=-" . $_GET['location'] .
"&radius=" . $_GET['radius'] .
"&sensor=" . $_GET['sensor'] .
"&key=" .$_GET['key']);
?>
It is probably best to check the input in the php files but this should work.
最好检查 php 文件中的输入,但这应该可行。
回答by bperreault
The places javascript API can be accessed here: http://code.google.com/apis/maps/documentation/javascript/places.html
可以在此处访问地点 javascript API:http: //code.google.com/apis/maps/documentation/javascript/places.html
回答by JB Nizet
A script loaded from one domain can't make an ajax request to another domain. Use the Google Maps JavaScript api to load your data.
从一个域加载的脚本不能向另一个域发出 ajax 请求。使用 Google Maps JavaScript api 加载您的数据。
回答by Julie
You can parse your object data this way:
您可以通过以下方式解析对象数据:
$(document).ready(function(){
$.getJSON('https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I', function(data) {
$.each(data, function(index,place) {
alert(place.name);
});
});
});
Hope it helps.
希望能帮助到你。