I'm wondering ,what I'm doing wrong here for two days. I Start the following AsyncTask GetLocationsAsync.java to send a HttpPost to a PHP file on a server,
public class GetLocationsAsyncTask extends AsyncTask<Map<String, String>, Object, ArrayList<HashMap<String, String>>>
{
public static final String USER_CURRENT_LAT = "user_current_lat";
public static final String USER_CURRENT_LONG = "user_current_long";
public static final String USER_CURRENT_ADDRESS = "user_current_address";
private Exception exception;
private ProgressDialog progressDialog;
private Context MainContext;
private GoogleMap mMap;
private MapActivity activity;
public GetLocationsAsyncTask(MapActivity activity, Context con)
{
super();
this.activity = activity;
this.MainContext = con;
}
public void onPreExecute()
{
progressDialog = new ProgressDialog(MainContext);
progressDialog.setMessage("Loading....");
progressDialog.show();
}
@Override
public void onPostExecute(ArrayList<HashMap<String, String>> result)
{
progressDialog.dismiss();
if (exception == null)
{
activity.loadLocations(result);
}
else
{
processException();
}
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Map<String, String>... params)
{
Map<String, String> paramMap = params[0];
try
{
double PLat = Double.valueOf(paramMap.get(USER_CURRENT_LAT));
double PLong = Double.valueOf(paramMap.get(USER_CURRENT_LONG));
String Addr = new String(paramMap.get(USER_CURRENT_ADDRESS));
Locq5ionsParser Lp = new LocationsParser();
Document doc = Pp.getDocument(Addr, PLat, PLong, "Bar");
ArrayList<HashMap<String, String>>LocationPoints = Lp.getDirection(doc);
return LocationPoints;
}
catch (Exception e)
{
exception = e;
Log.d("GetLocations", e.getMessage());
return null;
}
}
private void processException()
{
Toast.makeText(MainContext, "Fehler beim Auslesen der XML", Toast.LENGTH_LONG).show();
Log.d("GetLocationAsync", exception.getLocalizedMessage());
}
}
public class LocationParser {
public LocationParser() {
}
public Document getDocument(String addr, double latitude, double longitude, String type) {
String url = "http://**********/xmlphpshow.php";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(4);
postParameters.add(new BasicNameValuePair("Addresse", addr
.toString()));
postParameters.add(new BasicNameValuePair("latitude", String.valueOf(latitude)
.toString()));
postParameters.add(new BasicNameValuePair("longitude", String.valueOf(longitude)
.toString()));
postParameters.add(new BasicNameValuePair("type", "Bar"
.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
Log.d("LocationParser", e.getMessage());
e.printStackTrace();
return null;
}
}
public ArrayList<HashMap<String, String>> getDirection(Document DirektDoc) {
ArrayList<HashMap<String, String>> LocationItems = new ArrayList<HashMap<String, String>>();
NodeList nl, nl2, nl3;
// getting XML
if (DirektDoc.equals(null)){
Log.d("Dokument", "doc ist leer");
return null;
}
try{
nl = DirektDoc.getElementsByTagName("marker");
}catch(Exception e){
Log.d("TagName Markers", e.getMessage());
return null;
}
// looping through all item nodes <item>
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
try{
Node node1 = nl.item(i);
nl3 = node1.getChildNodes();
Node AddrNode = nl3.item(getNodeIndex(nl3, "address"));
String PAddr = AddrNode.getTextContent();
//the Log shows that the addresses of each marker item was read correctly
Log.d("LocationParser","Adresse :" + PAddr);
map.put("Addresse", PAddr);
Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
String lat = latNode.getTextContent();
map.put("latitude", lat);
Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
String lng = lngNode.getTextContent();
map.put("longitude", lng);
Node typeNode = nl3.item(getNodeIndex(nl3, "type"));
String LocType = typeNode.getTextContent();
map.put("Type", LocType);
Node timeNode = nl3.item(getNodeIndex(nl3, "tstamp"));
String timeString = timeNode.getTextContent();
map.put("TimeStamp", timeString);
LocationItems.add(map);
}catch(Exception e){
Log.d("LocationParser", e.getMessage());
}
}
return LocationItems;
}
private int getNodeIndex(NodeList NodeInl, String nodename) {
for (int i = 0; i < NodeInl.getLength(); i++) {
if (NodeInl.item(i).getNodeName().equals(nodename)){
return i;
}
}
return -1;
}
}
When I now try to read the Hashmap Values inside the ArrayList and create a Marker on my GoogleMap, there is only one Marker Visible.
public class MapActivity {
public GoogleMap googleMap;
public MapFragment mapfrag;
public Context MapContext;
public Document dc;
public MarkerOptions startMarker, ParkMarker;
public MapActivity(MapFragment gmapfrag, Context con){
this.mapfrag = gmapfrag;
this.googleMap = mapfrag.getMap();
this.MapContext = con;
}
public void initilizeMap() {
try {
googleMap.clear();
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
if (googleMap == null) {
Toast.makeText(MapContext, "Karte konnte nicht erstellt werden. Versuchen
sie es zu einem Späteren Zeitpunkt erneut.", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Toast.makeText(MapContext, "Karte konnte nicht erstellt werden. Versuchen
sie es zu einem Späteren Zeitpunkt erneut.", Toast.LENGTH_SHORT).show();
}
return;
}
public void loadMarkers(double Mylati, double Mylongi, String aDdR){
googleMap.clear();
startMarker = new MarkerOptions().position(new
LatLng(Mylati,Mylongi)).title("Aktuelle Position");
startMarker.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_icon));
googleMap.addMarker(startMarker);
Map<String, String> map = new HashMap<String, String>();
map.put(GetLocationsAsyncTask.USER_CURRENT_LAT, String.valueOf(Mylati));
map.put(GetLocationsAsyncTask.USER_CURRENT_LONG, String.valueOf(Mylongi));
map.put(GetLocationsAsyncTask.USER_CURRENT_ADDRESS, String.valueOf(aDdR));
GetLocationsAsyncTask asyncTask = new GetLocationsAsyncTask(this, MapContext);
asyncTask.execute(map);
//setCamView(Mylati, Mylongi);
return;
}
public void loadLocations(ArrayList<HashMap<String, String>> directionPoints){
for(int i = 0 ; i < directionPoints.size() ; i++){
String LocAddress = directionPoints.get(i).get("Addresse");
double LocLat = Double.parseDouble(directionPoints.get(i).get("latitude"));
double LocLng =
Double.parseDouble(directionPoints.get(i).get("longitude"));
String LocType = directionPoints.get(i).get("Type");
Log.d("MapActivit", "directionPoints.size beträgt: "+
directionPoints.size() + LocAddress + String.valueOf(i));
String LocTime = directionPoints.get(i).get("TimeStamp");
googleMap.addMarker( new MarkerOptions().position(new LatLng(LocLat,LocLng)).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon)));
}
return;
}
public void setCamView(double camLat, double camLong){
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(camLat, camLong)).zoom(12).build();
LatLng Startposition = new LatLng(camLat, camLong);
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return;
}
}
the generated XML file looks like following:
<markers>
<marker>
<address>1908 Pike pl, Seattle, WA</address>
<lat>47.610126</lat>
<lng>-122.342834</lng>
<type>Bar</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
<marker>
<address>1416 E Olive Way, Seattle, WA</address>
<lat>47.617214</lat>
<lng>-122.326584</lng>
<type>Bar</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
<marker>
<address>2234 2nd Ave, Seattle, WA</address>
<lat>47.613976</lat>
<lng>-122.345467</lng>
<type>Restaurant</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
<marker>
<address>1301 Alaskan Way, Seattle, WA</address>
<lat>47.605961</lat>
<lng>-122.340363</lng>
<type>Bar</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
<marker>
<address>1225 1st Ave, Seattle, WA</address>
<lat>47.606365</lat>
<lng>-122.337654</lng>
<type>Bar</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
</markers>
Can anybody help me? Whats wrong with my Code?
EDIT:
The Problem was solved thanks to gaurav5430 and Ivan. See their answers below.
EDIT:
// looping through all item nodes <item>
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
try{
Node node1 = nl.item(i);
nl3 = node1.getChildNodes();
...
...
should be
// looping through all item nodes <item>
HashMap<String, String> map=null;
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
map = new HashMap<String, String>();
try{
Node node1 = nl.item(i);
...
...
LocationItems.add(map);
ORIGINAL ANSWER (The OP has corrected this problem):
you seem to be adding the marker after the end of the loop
, so only the last marker
would be added
for(int i = 0 ; i < directionPoints.size() ; i++){
String LocAddress = directionPoints.get(i).get("Addresse");
double LocLat = Double.parseDouble(directionPoints.get(i).get("latitude"));
double LocLng =
Double.parseDouble(directionPoints.get(i).get("longitude"));
String LocType = directionPoints.get(i).get("Type");
Log.d("MapActivit", "directionPoints.size beträgt: "+
directionPoints.size() + LocAddress + String.valueOf(i));
String LocTime = directionPoints.get(i).get("TimeStamp");
Parkmarker = new MarkerOptions().position(new LatLng(LocLat,
LocLng)).title(LocAddress);
Parkmarker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon));
} //loop ends here
googleMap.addMarker(Parkmarker);
EDIT:
you have to instantiate a new ParkMarker
inside the loop every time, otherwise the same reference will get updated
EDIT: try to use
googleMap.addMarker( new MarkerOptions().position(new LatLng(LocLat,LocLng)).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon) );