Charan Reddy Charan Reddy - 1 month ago 13
Android Question

map on android device showing only one marker from url but showing all the values when I use Toast

I have tried many changes researching on google but my problem isn't resolved. I have an URL which contains following data. On map it should show more than one marker as I have 9 coordinates. But it is showing one marker with coordinates of last values of response. Any help can be appreciated.

{
"status":200,
"response":[
{
"docId":"1",
"docName":"Madan",
"docMobileNumber":"9676499774",
"location":"S R Nagar",
"specialization":"ENT",
"avaliablity":"Available",
"lat":"17.4436",
"log":"78.4458"
},
{
"docId":"2",
"docName":"Kumar",
"docMobileNumber":"9052598855",
"location":"KPHB",
"specialization":"Pediatrician",
"avaliablity":"Available",
"lat":"17.4948",
"log":"78.3996"
},
{
"docId":"3",
"docName":"charan",
"docMobileNumber":"8080809089",
"location":"Ameerpet",
"specialization":"Dentist",
"avaliablity":"Available",
"lat":"17.4375",
"log":"78.4483"
},
{
"docId":"4",
"docName":"Vamsy",
"docMobileNumber":"7777778888",
"location":"Kukatpally",
"specialization":"Orthopedic",
"avaliablity":"Available",
"lat":"17.4948",
"log":"78.3996"
},
{
"docId":"5",
"docName":"Ganesh",
"docMobileNumber":"9878686544",
"location":"Dilsuk Nagar",
"specialization":"Dermatologist",
"avaliablity":"Available",
"lat":"17.3688",
"log":"78.5247"
},
{
"docId":"6",
"docName":"Savitri",
"docMobileNumber":"8786599452",
"location":" West Marredpally",
"specialization":"Physician",
"avaliablity":"Not Available",
"lat":"17.4500",
"log":"78.5006"
},
{
"docId":"7",
"docName":"Sandhya",
"docMobileNumber":"9873243687",
"location":"Bowenpally",
"specialization":"Eye Specialist",
"avaliablity":"Available",
"lat":"17.898",
"log":"78.5008"
},
{
"docId":"8",
"docName":"Padma",
"docMobileNumber":"9768832418",
"location":"Kompally",
"specialization":"Cardiologist",
"avaliablity":"Not Available",
"lat":"17.5600",
"log":"78.5343"
},
{
"docId":"9",
"docName":"Priya",
"docMobileNumber":"9898767654",
"location":"Tirumalgiri",
"specialization":"Nerphrologist",
"avaliablity":"Available",
"lat":"17.787",
"log":"78.9805"
}
]
}


 

package com.example.charan.markermap;

import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class Maps3 extends FragmentActivity {

private GoogleMap mMap;

ArrayList<HashMap<String, Double>> locationList = new ArrayList<HashMap<String, Double>>();
JSONArray locations = new JSONArray();
int json1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

new TaskRead().execute();
}


public class TaskRead extends AsyncTask<Void, Void, Void> implements OnMapReadyCallback {

ProgressDialog pg;
Double latitude, longitude;

protected void onPreExecute() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
pg = ProgressDialog.show(Maps3.this, "Please Wait", "Conecting to Server");
pg.setCancelable(true);
}


protected Void doInBackground(Void... params) {
try {

String url = "http://192.168.1.33:8282/DocFinderServices/doctorService/doctorsList";

HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream inputStreamResponse = entity.getContent();

String str = convertStreamToString(inputStreamResponse);

if (str != null) {
try {
JSONObject jsonObj = new JSONObject(str);

// Getting JSON Array node
locations = jsonObj.getJSONArray("response");
json1 = jsonObj.getInt("status");

// looping through All records
for (int i = 0; i < locations.length(); i++) {
JSONObject c = locations.getJSONObject(i);

latitude = c.getDouble("lat");
longitude = c.getDouble("log");

// tmp hashmap for single contact
HashMap<String, Double> location = new HashMap<>();

// adding each child node to HashMap key => value
location.put("latitude", latitude);
location.put("longitude", longitude);
locationList.add(location);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(Void gmap) {
mMap.clear();
Marker[] allMarkers = new Marker[locationList.size()];
Toast.makeText(Maps3.this, "The list size is " + locationList.size(), Toast.LENGTH_SHORT).show();

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

if (mMap != null && json1 == 200) {
for (int j = 0; j < locationList.size(); j++) {
LatLng latLng = new LatLng(latitude, longitude);
allMarkers[i] = mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ok)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.0f));
mMap.getUiSettings().isZoomGesturesEnabled();
}

} else {
Toast.makeText(Maps3.this, "Oops..their is no map........", Toast.LENGTH_SHORT).show();
}
}
pg.dismiss();

}

public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sb.toString();
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

}
}
}

Answer

I have just changed LatLng latLng = new LatLng(latitude, longitude); to new values of getting key pair values as LatLng latLng = new LatLng(Double.valueOf(locationList.get(j).get("latitude")), Double.valueOf(locationList.get(j).get("longitude")));. Now it is working.