2013-12-10 47 views
0

我正在工作,現在在phonegap android應用程序中,我需要用戶的當前地址,以便我使用此 JSON api從javascript中獲取位置從JSON

這是我的代碼從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 

誰能告訴我,究竟哪裏的問題是什麼?

回答

0

如果我沒有記錯,PhoneGap中的默認安全策略是阻止所有網絡訪問。您需要將白名單http://maps.googleapis.com

+0

謝謝,我有加<訪問起源=「*」 />到配置文件,但還是同樣的結果... – Rohit

0
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() { 

    }) 
+0

感謝您的答覆,但沒有解決 - 一樣earliar – Rohit

+0

線264檢查你的代碼 – Ved

0

您請求不支持JSONP的API,最終的請求將是這樣的:

http://maps.googleapis.com/maps/api/geocode/json?callback=jQuery18309743240959942341_1386656119920&latlng=26.9008773,75.7403539&sensor=true&_method=GET&_=1386656120107 

與回調PARAM,但產量仍JSON,不javsscript文件中像

jQuery18309743240959942341_1386656119920(jsonDataHere...) 

看一看這個:https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse,它請求這樣的網址

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。

+0

我不我不想在地圖上顯示位置,我只是想在alert中顯示地址文本,我是否需要爲它找回... – Rohit

+0

關鍵部分是geocoder.geocode({'latLng':latlng},function(結果,狀態){//現在你得到結果 – Andrew