naveed abbas
naveed abbas

Reputation: 63

More than just Title and Snippet in a Marker Custom InfoWindow

I plot markers according to lat and lon that I receive from a web API through a JSON response.

The problem is that every marker has its own data set, for example each marker has an ID, code, phone number, and name. I would like to show all if this data in each Marker's InfoWindow using a custom InfoWindowAdapter, but I am unable to show relevant data that was received in the JSON.

Here is what I have tried so far. In this code, all marker snippet contain same data, and I want to add different data into this snippet for each Marker:

 private class GetFixture extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg) {


        ServiceHandler serviceClient = new ServiceHandler();
        Log.d("url: ", "> " + URL_ITEMS);
        String json = serviceClient.makeServiceCall(URL_ITEMS, ServiceHandler.GET);
        // print the json response in the log
        Log.d("Get match fixture resps", "> " + json);
        if (json != null) {
            try {
                Log.d("try", "in the try");
                JSONArray jsonArray = new JSONArray(json);
                Log.d("jsonObject", "new json Object");
                // Getting JSON Array node
                for (int i = 0; i <= jsonArray.length(); i++) {

                    JSONObject c = jsonArray.getJSONObject(i);
                    Double matchId = Double.parseDouble(c.getString(TAG_MATCHID));
                    Log.d("matchId", String.valueOf(matchId));
                    Double teamA = Double.valueOf(c.getString(TAG_TEAMA));
                    Log.d("teamA", String.valueOf(teamA));
                     teamB = c.getString(TAG_TEAMB);
                    Log.d("teamB", teamB);
                    //  hashmap for single match

                    HashMap<String, String> matchFixture = new HashMap<String, String>();
                    HashMap<String, String> cc = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    matchFixture.put(TAG_MATCHID, String.valueOf(matchId));
                    matchFixture.put(TAG_TEAMA, String.valueOf(teamA));
                    areahash.put(TAG_TEAMB, teamB);

                    matchFixtureList.add(new LatLng(matchId, teamA));
                    cc.put(TAG_TEAMB,teamB);
                    area.add(teamB);
                }
            } catch (JSONException e) {
                Log.d("catch", "in the catch");
                e.printStackTrace();
            }
        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }
        return null;
    }


    @Override
    protected void onPostExecute(Void result) {

        int x;
        //for (LatLng point : matchFixtureList) {
        //for (String a:area)
        for ( i = 0; i < matchFixtureList.size(); i++) {

            mMap.addMarker(new MarkerOptions().position(matchFixtureList.get(i)).title(area.get(i)));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(matchFixtureList.get(i), 15));
            mMap.animateCamera(CameraUpdateFactory.zoomIn());



            // Zoom out to zoom level 10, animating with a duration of 2 seconds.
            mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
            if (mMap != null) {
                mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                    @Override
                    public View getInfoWindow(Marker marker) {
                        return null;
                    }

                    @Override
                    public View getInfoContents(Marker marker) {
                        View v = getLayoutInflater().inflate(R.layout.check, null);
                        TextView ashid = (TextView) v.findViewById(R.id.ash_id);
                        TextView ashcode = (TextView) v.findViewById(R.id.ash_code);
                        TextView Cellno = (TextView) v.findViewById(R.id.cell_id);
                        TextView Ashname = (TextView) v.findViewById(R.id.ash_name);

                        TextView NIC = (TextView) v.findViewById(R.id.nic_id);
                        TextView tvlat = (TextView) v.findViewById(R.id.lat_id);
                        TextView tvLng = (TextView) v.findViewById(R.id.lonti_id);
                        TextView Zmid = (TextView) v.findViewById(R.id.ZM_id);




                        LatLng ll = marker.getPosition();
                        ashid.setText(marker.getTitle());
                        tvlat.setText(""+ll.latitude);
                        tvLng.setText("" + ll.longitude);

                        Zmid.setText("" + ll.longitude);
                        NIC.setText(""+ll.latitude);
                        Ashname.setText("" + ll.longitude);
                        Cellno.setText(""+ll.latitude);
                        ashid.setText("" + ll.longitude);
                        ashcode.setText(teamB);

                        ashcode.setText(marker.getSnippet());

                        return v;
                    }
                });


            }

            // mMap.addMarker(new MarkerOptions().position(pakistan).title("Marker in pakistn"));
            //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            // mMap.animateCamera(CameraUpdateFactory.zoomIn());
            // Zoom out to zoom level 10, animating with a duration of 2 seconds.
            // mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
            //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(matchFixtureList(i)));
            //mMap.animateCamera(CameraUpdateFactory.zoomTo(5));


        }
    }

image of snippet

Upvotes: 1

Views: 286

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

If you need to store more data points than just the title and snippet, the best way to solve this is to use a wrapper class to store the data for each Marker, and then keep the data stored in a HashMap with the Marker ID as the key so that you can obtain it in the InfoWindowAdapter.

First, create a holder class for the info corresponding to each Marker (you can expand on this to include more info as needed):

public class MarkerHolder {
    public String mAshId;
    public String mAshCode;
    public String mCellId;
    public String mAshName;

    public MarkerHolder(String ashId, String ashCode, String cellId, String ashName) {
        mAshId = ashId;
        mAshCode  = ashCode;
        mCellId = cellId;
        mAshName = ashName;
    }
}

Then create a HashMap<String, MarkerHolder> that will map each Marker ID to the data for each Marker, and make it an instance variable of the Activity:

HashMap<String, MarkerHolder> markerHolderMap = new HashMap<String, MarkerHolder>();

When you add each Marker, add an entry in the HashMap as well with a MarkerHolder object that stores all the data associated with this Marker:

Marker marker = mMap.addMarker(new MarkerOptions().position(matchFixtureList.get(i)).title(area.get(i)).snippet("snipp works"));

// Assuming these parameters come from your JSON:
MarkerHolder mHolder = new MarkerHolder(ashId, ashCode, cellId, ashName);

// Then add this holder reference to the HashMap
markerHolderMap.put(marker.getId(), mHolder); 

Then, in the InfoWindowAdapter, use the info obtained from the MarkerHolder which was obtained from the HashMap:

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

    @Override
    public View getInfoWindow(Marker arg0) {
        return null;
    }

    @Override
    public View getInfoContents(Marker arg0) {

        View v = getLayoutInflater().inflate(R.layout.check, null);
        TextView ashid = (TextView) v.findViewById(R.id.ash_id);
        TextView ashcode = (TextView) v.findViewById(R.id.ash_code);
        TextView cellNo = (TextView) v.findViewById(R.id.cell_id);
        TextView ashName = (TextView) v.findViewById(R.id.ash_name);

        //Get the MarkerHolder for this Marker:
        MarkerHolder markerHolder = markerHolderMap.get(arg0.getId());

        //Use the data to populate the layout:
        ashid.setText(markerHolder.mAshId);
        ashcode.setText(markerHolder.mAshCode);
        cellNo.setText(markerHolder.mCellId);
        ashName.setText(markerHolder.mAshName);

        //...........

        return v;
    }
});

Upvotes: 1

Related Questions