我是android新手。我正在开发一个应用程序,我从服务器获取纬度经度,我想在地图上显示这些点的标记。我正在使用二传手和吸气剂。但我得到相同的纬度和经度值重复,地图只显示一个标记。 这是我的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Intent intent = getIntent();
date = intent.getStringExtra("date");
empid = intent.getStringExtra("empid");
Log.e("Maspp acctt iddd", "" + empid);
Log.e("Maspp acctt dateeeee", "" + date);
mapp = new ArrayList<Pojo>();
new NetCheck().execute();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
private class NetCheck extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
map = supportMapFragment.getMap();
map.setMyLocationEnabled(true);
for (int i = 0; i < mapp.size(); i++) {
Double lati = Double.parseDouble(pojo.getLat().toString());
Double longi = Double.parseDouble(pojo.getLongg().toString());
String dattme = pojo.getDateTime().toString();
dest = new LatLng(lati, longi);
if (map != null) {
map.addMarker(new MarkerOptions().position(dest));
map.moveCamera(CameraUpdateFactory.newLatLng(dest));
map.animateCamera(CameraUpdateFactory.zoomTo(15));
map.addMarker(new MarkerOptions()
.position(dest)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.title(""+dattme));
}
}
nPregress.setVisibility(View.GONE);
return;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
Log.e("beforeeeee", "");
HttpPost httpRequest = new HttpPost(
"http://url");
httpRequest.setHeader("Content-Type", "application/json");
Log.e("Afetererer", "");
JSONObject json = new JSONObject();
json.put("login_date", date);
json.put("empid", empid);
Log.i("JSON Object", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpRequest.setEntity(se);
HttpResponse httpRes = httpClient.execute(httpRequest);
java.io.InputStream inputStream = httpRes.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
strServerResponse = sb.toString();
Log.e("Server Response", "" + strServerResponse.toString());
Log.e("priyankaaaa","priyuaaaaaa");
if (strServerResponse != null) {
try {
JSONArray arr = new JSONArray(strServerResponse);
Log.e("priyankaaa111a","priyuaa111aaaa");
JSONObject jsonObj = arr.getJSONObject(0);
for (int i = 0; i < arr.length(); i++) {
pojo = new Pojo();
String latitude = jsonObj.optString("Latitude");
String longitude = jsonObj.optString("Longitude");
String date_time = jsonObj.optString("date_time");
Log.e("Latitudeeeeee",""+latitude);
Log.e("longfffff",""+longitude);
Log.e("dateeee",""+date_time);
pojo.setLat(latitude);
pojo.setLongg(longitude);
pojo.setDateTime(date_time);
mapp.add(pojo);
}
答案 0 :(得分:1)
错误在于你没有在每个索引处获得json对象,而是只得到第一个。
因此,将json对象放在所需的索引处,然后解析数据。