Question

I'm trying to display a few points (points of interest) with custom InfoWindow in google maps android. My problem is that I cannot put different information in the different points. I'm facing problems updating the textview content of the popup layout. See my sample code below.

My infoWindowAdapter code:

public class poisInfoWindowAdapter implements InfoWindowAdapter {

      @Override
      public View getInfoWindow(Marker arg) {

    return null;

   }

   @Override
   public View getInfoContents(Marker marker) {

   //Get Layout of POI's popups and assign values to text views.
   View InfoPopupLayout = getLayoutInflater().inflate(R.layout.infopopup, null);

   TextView t = ((TextView)InfoPopupLayout.findViewById(R.id.title));
   t.setText(name);

       return InfoPopupLayout;

   }
    }

The code responsable for adding Points to map:

    public void onPostExecute(String responsePois) {

    try {
         JSONArray P = new JSONArray(responsePois);

            for (int i = 0; i < P.length(); i++) {
                JSONObject pois = P.getJSONObject(i);
                position = new LatLng(pois.getDouble("y"), pois.getDouble("x"));
                name = pois.getString("name_pt");




            Map.setInfoWindowAdapter(new poisInfoWindowAdapter());

            Map.addMarker(new MarkerOptions()
                        .position(position)
                    .title(name)
                   );
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

In this case all of my points get the same name. The infoWindowAdapter class can't get the correct values from name variable. Anyone knows how to resolve this problem? I think that what I say and how I explain is understandable but if not, tell me and I will answer.

Thanks

Was it helpful?

Solution

First of all put your Map.setInfoWindowAdapter(new poisInfoWindowAdapter()); outside your for loop in onPostExecute(....)

and second implement your poisInfoWindowAdapter() like below:

 public class poisInfoWindowAdapter implements InfoWindowAdapter {

  @Override
  public View getInfoWindow(Marker arg) {
  return null;
}
@Override
public View getInfoContents(Marker marker) {

//Get Layout of POI's popups and assign values to text views.
View InfoPopupLayout = getLayoutInflater().inflate(R.layout.infopopup, null);

TextView t = (TextView)InfoPopupLayout.findViewById(R.id.title);
t.setText(marker.getTitle());

TextView t2 = (TextView)InfoPopupLayout.findViewById(R.id.title2);
t2.setText(marker.getSnippet());

    return InfoPopupLayout;
  }
 }

Update: set Marker as

Currnt = mMap.addMarker(new MarkerOptions()
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red))
    .position(new LatLng(latitude,longitude)
    .title(locations)
            .snippet(city));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top