【发布时间】:2014-07-18 08:30:34
【问题描述】:
我正在尝试从 json 在 android 中创建谷歌地图,但无法从 AsyncTask 获取 onPostExecute 中的 latlng 值。我是新手。 这是我的代码。
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String ID="id";
static String NAME = "name";
static String LAT="lat";
static String LNG="lng";
@Override
protected String doInBackground(String... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("https://sitename.com/storeListJson.php");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("contacts");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("id", jsonobject.getString("id"));
map.put("name", jsonobject.getString("name"));
map.put("lat",jsonobject.getString("lat"));
map.put("lng",jsonobject.getString("lng"));
String latt=jsonobject.getString(LAT);
String lngg=jsonobject.getString(LNG);
// Set the JSON Objects into the arrayddf
// arraylist.add(map);
Log.d("Location", "Location:" + latt + " " + lngg);
//above line shows me the lat/lng values quite right in logcat
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result)
{
//what to write here, tried all things but cant get lat/lng vals
}
我尝试了很多方法,通常得到空白地图,然后是 NPE 错误,导致 onPostExecute 缺少纬度/经度值。
Log.d("Location", "Location:" + latt + " " + lngg);
上面一行显示了我在 doInBackground 中执行的 logcat 中的 lat/lng 值。
我是 android 的新手,希望 SO 社区能帮助我引导我走上正确的道路。
【问题讨论】:
标签: android google-maps