Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Along with title and snippet how to add extra data to marker?

Problem : My remote server returns 10 multiple value for a request.i parsed my response and using for loop i added markers(here i added info to title and snippet) on a map.(here i want to add extra data to marker so that i can access it in on info window click event)

in infoWindowClickListener i want to access that extra data.

How to add extra data to marker/ how to access ph data for a particular marker click(other wise i will get last value of ph in all markers).

i tried like this.

   private class HttpGetTask extends AsyncTask<Void, Void, String> 
    {
    //URL and http stuff
    }

            @Override
            protected void onPostExecute(String result) {

                try {
                     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");

                        Log.v("POINT", point);//Checking points

                        String phone1 = e.getString("ph");
                        Log.v("PH", phone1);//Checking phone numbers

                        String[] point2 = point.split(",");//Splitting points
                        double lat1 = Double.parseDouble(point2[0]);
                        double lng1 = Double.parseDouble(point2[1]);
                        Log.v("LLDN", "" + lat1 + "&" + lng1);

                        //Adding multiple markers
//can i add extra information here along with title and snippet
                        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)));

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

is it possible to attach extra value to a marker, along with title and snippet.? or am i parsing in wrong way.?

like image 566
Aparichith Avatar asked Apr 23 '14 07:04

Aparichith


2 Answers

While @worawee.s's will work perfectly fine, there is a more elegant solution that can allow you to add additional informations to a Marker object. So to solve this, you can use Marker's setTag (Object tag) method:

You can use this property to associate an arbitrary Object with this marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map. As another example, you can associate a String ID corresponding to the ID from a data set.

like image 89
Alex Mamo Avatar answered Oct 12 '22 00:10

Alex Mamo


Not the best solution but this what I do in my application.

create markersMap as a private field in your activity/fragment.

private Map<Marker, ExtraDataObj> markersMap = new HashMap<Marker, ExtraDataObj>();

When you generate marker also put the marker and extra data in your markersMap

ExtraDataObj extraDataObj = new ExtraDataObj();
// extract and store all data you want in the extraDataObj
....
...
..
Marker marker = 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)));
markersMap.put(marker, extraDataObj);

in your onInfoWindowClick get extra data from the markersMap

ExtraDataObj extraDataObj = markersMap.get(arg0) 
like image 38
worawee.s Avatar answered Oct 12 '22 00:10

worawee.s