从javascript中的JSON获取位置
Get location from JSON in javascript
我现在在一个phonegap android应用程序中工作,我需要用户的当前地址,所以我使用了这个JSON api。
这是我从JSON api 获取位置更新数据的代码
$.ajax('http://maps.googleapis.com/maps/api/geocode/json?latlng=26.9008773,75.7403539&sensor=true', {
dataType: 'jsonp',
timeout: 3000,
data: {
_method: 'GET'
},
success: function (data,status) {
if (data._status != 200) {
console.log('received error', data._status);
// handle error
}else{
console.log('received data', data);
// do something useful with the data
}
},
error: function(data,status) {
var myObject = JSON.stringify(data);
console.log("Data Returned is " + myObject);
console.log("Status is " + status);
alert('error');
},
complete: function(data, status) {
alert('complete');
}
});
每次它进入错误部分,然后是完整部分,永远不会进入成功部分。控制台输出为:
12-10 11:11:34.393: I/Web Console(10620): Data Returned is {"readyState":4,"status":404,"statusText":"error"} at file:///android_asset/www/index.html:263
12-10 11:11:34.393: I/Web Console(10620): Status is error at file:///android_asset/www/index.html:264
有人能告诉我,问题到底在哪里吗?
如果我没记错的话,PhoneGap中的默认安全策略是阻止所有网络访问。您需要将http://maps.googleapis.com
列入白名单。
您请求的api不支持jsonp,最终请求如下:
http://maps.googleapis.com/maps/api/geocode/json?callback=jQuery18309743240959942341_1386656119920&latlng=26.9008773,75.7403539&sensor=true&_method=GET&_=1386656120107
使用回调参数,但输出仍然是json,而不是像那样的javsscript文件
jQuery18309743240959942341_1386656119920(jsonDataHere...)
看看这个:https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse,它像这个一样请求url
https://maps.googleapis.com/maps/api/js/GeocodeService.Search?5m2&1d40.714224&2d-73.96145200000001&7sUS&9sen&callback=_xdc_._4cni6z&token=46423
输出为:
_xdc_._4cni6z && _xdc_._4cni6z( { ....
那是jsonp。
ajax_call ='http://maps.googleapis.com/maps/api/geocode/json?latlng=26.9008773,75.7403539&sensor=true';
$.ajax({
type: "GET",
url: ajax_call,
cache:false,
dataType: "json",
success: function(response){
$.each(response.results, function(key, value) {
//alert(value.formatted_address);
console.log(value.formatted_address);
});
}
}).done(function() {
})
相关文章:
- 如何获取位置-php
- 如何在ModestMaps / Wax中获取位置点击事件
- 如何在重定向 URL 的标头中拦截和获取位置
- 从javascript中的JSON获取位置
- 获取位置A的位置并计算其到位置B的距离
- 内容可编辑获取位置,包括HTML
- 网格堆栈.js从 JSON 获取位置
- 你如何从引荐来源网址获取位置.hashtag - 谷歌分析
- 无法使用 JavaScript 获取位置信息
- 无法从必应地图获取位置详细信息
- Javascript/Jquery在循环中获取位置的速度很慢
- 从新创建的窗口中获取位置主机名
- 尝试使用谷歌API v3从地址获取位置
- Javascript或PHP获取位置并将其发送给我
- JavaScript 或 JQuery:获取位置上最内层的元素
- 使用Google API获取位置的纬度和经度,并传递变量
- 我可以't从bufferGeometry中获取位置属性
- 可以't获取位置&点击iPad获取容器信息
- 告知用户是否已授予网站在浏览器中获取位置的永久权限
- 如何从谷歌位置api获取位置的照片