I am using google map api.In my case i am receiving latitude,longitude and username from mysql through php.I am receiving all users collectively in one time.I try following code it gives only one marker.Plz guide me. And don't make it duplicate because i tried many solutions but not working for me.If u have no answer please quit.Please Rec.class
public class Rec extends AppCompatActivity{
private GoogleMap mMap;
JSONObject jsonObject = new JSONObject();
JSONArray result = new JSONArray();
ArrayList<HashMap<String,String>> list = new ArrayList<>();
private String latitude,longitude,user_name;
Button btn;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rec_gps);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recGps();
}
});
}
private void recGps() {
loading = ProgressDialog.show(this, "Please wait ......", "fetching....", false, false);
String url = Config.URL_CARD_INFO;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RecGPS.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(final String response) {
try {
jsonObject = new JSONObject(response);
result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
for (int i=0;i<result.length();i++) {
JSONObject collegeData = result.getJSONObject(i);
user_name=collegeData.getString(Config.TAG_NAME);
latitude = collegeData.getString(Config.TAG_ISSUE_DATE);
longitude = collegeData.getString(Config.TAG_Expiry_DATE);
HashMap<String,String> users = new HashMap<>();
users.put(Config.TAG_NAME,user_name);
users.put(Config.TAG_ISSUE_DATE,latitude);
users.put(Config.TAG_Expiry_DATE,longitude);
list.add(users);
}
} catch (JSONException e) {
e.printStackTrace();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getMaxZoomLevel();
for(int i=0;i<result.length();i++) {
mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(list.get(i).get(Config.TAG_ISSUE_DATE)), Double.parseDouble(list.get(i).get(Config.TAG_Expiry_DATE)))).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).title(list.get(i).get(Config.TAG_NAME)));
}
}
});
}
}
Bro Your Code is Perfectly Right for adding markers.I think you have problem in data fetching from MySql like php or any other source..please check that. And also update your code with following to get focus on markers.
for (int i = 0; i < result.length(); i++) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(list.get(i).get(Config.TAG_ISSUE_DATE)), Double.parseDouble(list.get(i).get(Config.TAG_Expiry_DATE))),list.size()));
mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(list.get(i).get(Config.TAG_ISSUE_DATE)), Double.parseDouble(list.get(i).get(Config.TAG_Expiry_DATE)))).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).title(list.get(i).get(Config.TAG_NAME)));
}