Javascript 如何从Bing地图API获取JSON?
我想使用Bing Maps API使用Javascript获取位置或地址的坐标 我生成了如下请求: 当我手动打开链接时,我看到一个JSON文件,如下所示: 但是我怎样才能获取这个文件来获取坐标呢 我尝试了下面的代码,在控制台上显示数据,但什么也没发生Javascript 如何从Bing地图API获取JSON?,javascript,api,bing-maps,bing-api,Javascript,Api,Bing Maps,Bing Api,我想使用Bing Maps API使用Javascript获取位置或地址的坐标 我生成了如下请求: 当我手动打开链接时,我看到一个JSON文件,如下所示: 但是我怎样才能获取这个文件来获取坐标呢 我尝试了下面的代码,在控制台上显示数据,但什么也没发生 var req=新请求(请求); 获取(请求) .然后(功能(响应){ 返回response.text(); }) .然后(函数(文本){ console.log(文本); });您得到的结果如下表所示 { "resourceS
var req=新请求(请求);
获取(请求)
.然后(功能(响应){
返回response.text();
})
.然后(函数(文本){
console.log(文本);
});代码>您得到的结果如下表所示
{
"resourceSssSets":{
"0": {
"estimatedTotal": 1,
"resources" :{
"0" :{
"__type" : "location....",
"bbox" {
"0": "000",
"1": "000",
"3": "000",
"4": "0000",
"name": "name"
},
"point":{
"type": "point",
"coordinates": {
"0": "",
"1": ""
},
"address": {
}
}
}
}
}
}
}
json数组
现在,您可以查看想要收集的内容了
let data=fetch('http://dev.virtualearth.net/REST/v1/Locations?CountryRegion=DE&locality=Munich&addressLine=1%20Schloss%20Nymphenburg&maxResults=1&key=%7BmyKEY%7D')
.then(response=>console.log(“res:,response))
.catch(err=>console.log(“err:,err));
console.log(数据)
//console.log(数据[“ResourcesSSets”][“0”][“resources”][“0”][“point”][“coordinates”])
/*
{
“资源集”:{
"0": {
“资源”:{
"0" :{
“要点”:{
“坐标”:{
"0": "",
"1": ""
}
}
}
}
}
}
}
*/
以下是如何提出请求并处理结果:
var req = 'http://dev.virtualearth.net/REST/v1/Locations?CountryRegion=DE&locality=Munich&addressLine=1%20Schloss%20Nymphenburg&maxResults=1&key={yourKey}';
fetch(req)
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(JSON.stringify(data));
if(data.resourceSets && data.resourceSets.length > 0 && data.resourceSets[0].resources && data.resourceSets[0].resources.length > 0){
var firstResult = data.resourceSets[0].resources[0];
var latitude = firstResult.point.coordinates[0];
var longitude = firstResult.point.coordinates[1];
//Do something with this data.
}
})
注意,我将URL作为字符串传递,并调用response.json()而不是response.text()“但什么也没发生”-没有错误消息?var req=new Request(Request)代码>-请求
在那里没有定义。只是什么都没有发生,也没有错误消息。我在代码片段之前定义了变量请求,其中包含链接。是否存在一种解决方法,比如在后台打开链接,然后读取数据?