在android中将Lat/Lng传递给onPostExecute
我正试图从json在android中创建google地图,但无法从AsyncTask获取onPostExecute中的latlng值。我是新手。 这是我的密码在android中将Lat/Lng传递给onPostExecute,android,google-maps,Android,Google Maps,我正试图从json在android中创建google地图,但无法从AsyncTask获取onPostExecute中的latlng值。我是新手。 这是我的密码 // Declare Variables JSONObject jsonobject; JSONArray jsonarray; ListView listview; ListViewAdapter adapter; ProgressDialog mProgressDialog; ArrayList<HashMap&l
// 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
}
上面的行显示了我在doInBackground中执行的logcat中非常正确的lat/lng值
我是android新手,希望社区能帮助我找到正确的方法。你只需要在
doInBackground()方法中传递latt和lngg的值,而不是null
@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 arraylist;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> results)
{
//Do what you want with the results here
}
@覆盖
受保护的字符串doInBackground(字符串…参数){
//创建一个数组
arraylist=新的arraylist();
//从给定的URL地址检索JSON对象
jsonobject=JSONfunctions
.getJSONfromURL(“https://sitename.com/storeListJson.php");
试一试{
//在JSON中找到数组名称
jsonarray=jsonobject.getJSONArray(“联系人”);
for(int i=0;i
您还必须转到扩展AsyncTask
的部分,并对其进行更改,使其还包括arraylist
,类似于扩展AsyncTask
,感谢您的指导。我已经能够在onPostExecute中获取lat/lng(LogCat显示所有条目)。然而,当我试图绘制地图时,什么也没发生<代码>设置映射();对于(inti=0;i
我看不到任何标记,logcat显示1 latlngI终于能够用标记显示地图。我一直在两个地方调用Mapinitialize函数,第二个地方调用了code语句。可能忘了我已经调用了两次。非常感谢您的支持。OS Community rocks!请告诉我如何使用n我只显示符合用户半径限制的20个位置。我知道它需要位置侦听器,但不知道从哪里开始。可能有很多种方法可以做到这一点。这里有一些关于高级地图功能的指南:另外,关于位置的文档非常好:@Shadesblade我需要google maps v2中的帮助,我需要使用json解析将LatLng的值传递给具有json对象LatLng的jsonarray。你能指导我吗
@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 arraylist;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> results)
{
//Do what you want with the results here
}