我正在尝试使用Google Maps android中的自定义InfoWindow显示一些点(兴趣点)。我的问题是我不能在不同的地方放置不同的信息。我在更新弹出式布局的textview内容时遇到问题。请参阅下面的示例代码。

我的infoWindowAdapter代码:

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;

   }
    }


负责添加地图点的代码:

    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();
        }

    }


在这种情况下,我的所有积分都具有相同的名称。 infoWindowAdapter类无法从名称变量中获取正确的值。有人知道如何解决这个问题吗?
我认为我所说的和我的解释是可以理解的,但如果不能,请告诉我,我会回答。

谢谢

最佳答案

首先将Map.setInfoWindowAdapter(new poisInfoWindowAdapter());放在onPostExecute(....)的for循环之外

第二个实现您的poisInfoWindowAdapter(),如下所示:

 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;
  }
 }


更新:将Marker设置为

Currnt = mMap.addMarker(new MarkerOptions()
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red))
    .position(new LatLng(latitude,longitude)
    .title(locations)
            .snippet(city));

07-28 02:20
查看更多