要改回可读格式,您也可以使用Geoprer,但有时由于Google Play服务问题而无法正常工作。我将这个json地理编码用作第二种选择,以防万一。
请参考 Google Geocoding
API
工作流程是通过您的纬度和经度并获取当前位置。请求网址将是这样。
String reqURL = "http://maps.googleapis.com/maps/api/geopre/json?latlng="+ lat+","+lng +"&sensor=true";
希望这个答案对您有帮助。
public static JSonObject getLocationInfo(double lat, double lng) { HttpGet httpGet = new HttpGet("http://maps.googleapis.com/maps/api/geopre/json?latlng="+ lat+","+lng +"&sensor=true"); HttpClient client = new DefaultHttpClient(); HttpResponse response; StringBuilder stringBuilder = new StringBuilder(); try { response = client.execute(httpGet); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); int b; while ((b = stream.read()) != -1) { stringBuilder.append((char) b); } } catch (ClientProtocolException e) { } catch (IOException e) { } JSonObject jsonObject = new JSonObject(); try { jsonObject = new JSonObject(stringBuilder.toString()); } catch (JSonException e) { e.printStackTrace(); } return jsonObject;}public static String getCurrentLocationViaJSON(double lat, double lng) { JSonObject jsonObj = getLocationInfo(lat, lng); Log.i("JSON string =>", jsonObj.toString()); String currentLocation = "testing"; String street_address = null; String postal_pre = null; try { String status = jsonObj.getString("status").toString(); Log.i("status", status); if(status.equalsIgnoreCase("OK")){ JSonArray results = jsonObj.getJSonArray("results"); int i = 0; Log.i("i", i+ "," + results.length() ); //TODO delete this do{ JSonObject r = results.getJSonObject(i); JSonArray typesArray = r.getJSonArray("types"); String types = typesArray.getString(0); if(types.equalsIgnoreCase("street_address")){ street_address = r.getString("formatted_address").split(",")[0]; Log.i("street_address", street_address); }else if(types.equalsIgnoreCase("postal_pre")){ postal_pre = r.getString("formatted_address"); Log.i("postal_pre", postal_pre); } if(street_address!=null && postal_pre!=null){ currentLocation = street_address + "," + postal_pre; Log.i("Current Location =>", currentLocation); //Delete this i = results.length(); } i++; }while(i<results.length()); Log.i("JSON Geo Locatoin =>", currentLocation); return currentLocation; } } catch (JSonException e) { Log.e("testing","Failed to load JSON"); e.printStackTrace(); } return null;}
根据我的经验, 只有设备生成的纬度和经度 会起作用。然后打电话
String currentLocation = getCurrentLocationViaJSON(lat, lng);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)