shubham shubham - 2 months ago 14
Android Question

how to show multiple markers in map from sqlite

m getting different users latLng as a json object and m storing and updating it in sqlite with that user name , now i want to retrive that lacations of users and show these users in to map.. and update their location as message arrived af that user... how to do that

this is my code

JSONObject jsnobject = new JSONObject(Message);
JSONObject msg = jsnobject.getJSONObject("msg");

JSONObject header = msg.getJSONObject("Header");
clientID=header.getString("from");


JSONObject location = header.getJSONObject("Location");
LAT = location.getString("Lat");
LNG = location.getString("Long");
SPEED = location.getString("Speed");

} catch (JSONException e) {
e.printStackTrace();
}






// for(int i =0 ;i<abc;i++){


List<String> a= db.getID();

if(a.contains(clientID)){

db.update(clientID, LAT, LNG, SPEED);
}else{

db.insert(clientID, LAT, LNG, SPEED);

}


and this is how m placing the marker

List<String> a= db.getID();

for(int i=0;i<a.size();i++){



String id=a.get(i);

List<String> latllng= db.getLatLng(id);
latllng.toString();
String[] Latlng=latllng.toString().replace("[", "").replace("]", "").split(",");
double latitude = Double.parseDouble(Latlng[0]);
double longitude = Double.parseDouble(Latlng[1]);
googleMap.clear();


if (marker == null)
pos=new LatLng(latitude,longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
pos, 17));
marker = googleMap.addMarker(new MarkerOptions()
.position(pos));
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker));
marker.setTitle(id);
pos = new LatLng(latitude, longitude);

animateMarker(marker, pos, hideMarker);

}

Answer

Update your code like this and it will work:

    List<String> a = db.getID();
    googleMap.clear();
        for (int i = 0; i < a.size(); i++) {

            String id = a.get(i);

            List<String> latllng = db.getLatLng(id);
            latllng.toString();
            String[] Latlng = latllng.toString().replace("[", "")
                    .replace("]", "").split(",");
            double latitude = Double.parseDouble(Latlng[0]);
            double longitude = Double.parseDouble(Latlng[1]);
            // googleMap.clear(); <<< comment out this code else all of your previous marker get cleared

            if (marker == null)
                pos = new LatLng(latitude, longitude);
             // googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos, 17)); << you can not move map for each and every marker you are adding
            marker = googleMap.addMarker(new MarkerOptions().position(pos));
            marker.setIcon(BitmapDescriptorFactory
                    .fromResource(R.drawable.my_marker));
            marker.setTitle(id);
            pos = new LatLng(latitude, longitude);
            animateMarker(marker, pos, hideMarker); 

        }