Question

i am working on an app, where i am getting response(Json) from my remote server.I am also able to parse and place markers based on that response.But i am failing in parsing ph data from response and passing it into another activity.

It is sending only phone number of last json object data in setOnInfoWindowClickListener event.

I know some minor modifications i have to made. Please suggest me in this.

This is the Json response i am getting.

[
{
id: 965,
distance: "1.47",
ph: "33441111",
name: "XYZ",
LS: " abcdef",
point: "77.588018,12.959282"
},
{
id: 965,
distance: "1.47",
ph: "33441111",
name: "XYZ",
LS: " abcdef",
point: "77.588018,12.959282"
},
.
.
]

I tried this way to parse

private class HttpGetTask extends AsyncTask<Void, Void, String> {

        // Showing progress dialog
        // Passing URL

    @Override
    protected String doInBackground(Void... params) {

        // http stuff
    }

    @Override
    protected void onPostExecute(String result) {

        if (pDialog.isShowing())
            pDialog.dismiss();
        try {
            JSONArray json = new JSONArray(result);

            for (int i = 0; i < json.length(); i++) {
                Log.v("Response", result);
                final JSONObject e = json.getJSONObject(i);
                String point = e.getString("point");

                final String phone = e.getString("ph");                 

                String[] point2 = point.split(",");
                double lat1 = Double.parseDouble(point2[0]);
                double lng1 = Double.parseDouble(point2[1]);

                gMap.addMarker(new MarkerOptions()
                        .title(e.getString("name"))
                        .snippet(
                                e.getString("LS") + "*" + e.getString("ph"))
                        .position(new LatLng(lng1, lat1))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.pmr)));

                gMap.setInfoWindowAdapter(new InfoWindowAdapter() {

                    @Override
                    public View getInfoWindow(Marker arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public View getInfoContents(Marker mrkr) {
                        // TODO Auto-generated method stub
                        String name = mrkr.getTitle();
                        String detail = mrkr.getSnippet();
                        String trimmedDetail = detail.substring(0, 60);

                        Log.v("Info", name + " " + detail);
                        View v = getLayoutInflater().inflate(
                                R.layout.infowindow, null);
                        TextView title = (TextView) v
                                .findViewById(R.id.titleTV);
                        TextView snippet = (TextView) v
                                .findViewById(R.id.snippetTV);

                        title.setText("" + name);
                        snippet.setText("" + trimmedDetail);

                        return v;
                    }
                });

                gMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker arg0) {

                        Intent myIntent = new Intent(getBaseContext(),
                                DtActivity.class);
                        myIntent.putExtra("title", arg0.getTitle());
                        myIntent.putExtra("detail", arg0.getSnippet());
                        myIntent.putExtra("ph1", phone);

// How to access and send ph here.

                        try {
                            String ph = e.getString("ph");
                            myIntent.putExtra("ph", ph);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        startActivity(myIntent);
                    }
                });

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (null != mClient)
            mClient.close();

    }
}
Was it helpful?

Solution

You need to Initialize the gMap Globaly and add the marker inside the for Loop. Remove the adapter and infowindow click listener codes from form loop and paste it outside. Kindly check the below sample.

public class Example extends FragmentActivity implements OnInfoWindowClickListener {

public static GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);
    mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    mMap.setOnInfoWindowClickListener(this);
    // Inside the Loop

    mMap.addMarker(new MarkerOptions().title(e.getString("name"))
            .snippet(e.getString("LS") + "*" + e.getString("ph"))
            .position(new LatLng(lng1, lat1))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pmr)));
    // Close the loop

}

@Override
public void onInfoWindowClick(Marker marker) {
    // here you can get title, snippet, lat,lng of selected marker. then you
    // can start activity.
}

}

Kindly paste the for loop inside the onPostExecute of your Asyntask Class.

OTHER TIPS

Add this code snippet to get the phone number.I have marked it with you will get your phone number here

gMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker arg0) {

  ///////////////you will get your phone number here////////////////////////                      

 for (int j = 0; j < json.length(); j++) {
JSONObject jo = json.getJSONObject(j);
if(arg0.getTitle().equals(jo.getString("title"))){
String phone=  jo.getString("ph");

}


///////////////you will get your phone number here////////////////////////         


Intent myIntent = new Intent(getBaseContext(),
                                DtActivity.class);
                        myIntent.putExtra("title", arg0.getTitle());
                        myIntent.putExtra("detail", arg0.getSnippet());
                        myIntent.putExtra("ph1", phone);



                        startActivity(myIntent);
                    }
                });

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

The problem is that you are setting a new setInfoWindowAdapter and setOnInfoWindowClickListener each time you iterate to ur for loop and the last iterate or the last json object will be the InfoWindowAdapter and InfoWindowClickListener that why you are getting the last json, you can only set it once not multiple times except for the add marker.

Dont put the setInfoWindowAdapter and setOnInfoWindowClickListener in the for loop

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top