InfoWindow标题不在onMarkerDrag上更新

时间:2014-03-15 07:31:54

标签: android google-maps maps google-maps-android-api-2

我正在尝试在拖动标记时更新我的​​InfoWindow标题。但是,尽管代码已被点击,但不会更新信息。

代码:

import android.app.Activity;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.myapp.app.R;

/**
 * This class creates a Custom a InfoWindowAdapter that is used to show
 * popup on map when user taps on a pin on the map. Current implementation
 * of this class will show a Title and a snippet.
 * 
 */
 public class CustomInfoWindowAdapter implements InfoWindowAdapter,OnMarkerDragListener {

    /** The contents view. */
    private final View mContents;

    /**
     * Instantiates a new custom info window adapter.
     */
    public CustomInfoWindowAdapter(Activity A) {

        mContents = A.getLayoutInflater().inflate(
                R.layout.map_popup, null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.google.android.gms.maps.GoogleMap.InfoWindowAdapter#getInfoWindow
     * (com.google.android.gms.maps.model.Marker)
     */
    @Override
    public View getInfoWindow(Marker marker) {

        render(marker, mContents);
        return mContents;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.google.android.gms.maps.GoogleMap.InfoWindowAdapter#getInfoContents
     * (com.google.android.gms.maps.model.Marker)
     */
    @Override
    public View getInfoContents(Marker marker) {

        return null;
    }

    /**
     * Render the marker content on Popup view. Customize this as per your
     * need.
     * 
     * @param marker
     *            the marker
     * @param view
     *            the content view
     */
    private void render(final Marker marker, View view) {

        String title = marker.getTitle();
        TextView titleUi = (TextView) view.findViewById(R.id.title);
        if (title != null) {
            SpannableString titleText = new SpannableString(title);
            titleText.setSpan(new ForegroundColorSpan(Color.BLACK), 0,
                    titleText.length(), 0);
            titleUi.setText(titleText);
        } else {
            titleUi.setText("");
        }

        String snippet = marker.getSnippet();
        TextView snippetUi = (TextView) view.findViewById(R.id.snippet);
        if (snippet != null) {
            SpannableString snippetText = new SpannableString(snippet);
            snippetText.setSpan(new ForegroundColorSpan(Color.BLACK), 0,
                    snippet.length(), 0);
            snippetUi.setText(snippetText);
        } else {
            snippetUi.setText("");
        }



    }

    @Override
    public void onMarkerDrag(Marker arg0) {
        // TODO Auto-generated method stub
        LatLng position = arg0.getPosition();

        arg0.setTitle("HAHAH"); //DOES NOT WORK

        Log.d(getClass().getSimpleName(), String.format("Dragging to %f:%f",
                position.latitude, position.longitude));//HITS FINE
    }

    @Override
    public void onMarkerDragEnd(Marker arg0) {
        // TODO Auto-generated method stub
        LatLng position = arg0.getPosition();
        String title = "Position A";
        TextView titleUi = (TextView) getInfoWindow(arg0).findViewById(R.id.title);
        if (title != null) {
            SpannableString titleText = new SpannableString(title);
            titleText.setSpan(new ForegroundColorSpan(Color.BLACK), 0,
                    titleText.length(), 0);
            Log.d(getClass().getSimpleName(), String.format("earlier title:%s  now should be:%s",
                    titleUi.getText(),titleText));//HITS FINE
            titleUi.setText(titleText); //DOES NOT WORK EITHER
        }


        Log.d(getClass().getSimpleName(), String.format("Dragged to %f:%f",
                position.latitude, position.longitude)); //HITS FINE
    }

    @Override
    public void onMarkerDragStart(Marker arg0) {
        // TODO Auto-generated method stub
        LatLng position = arg0.getPosition();

        Log.d(getClass().getSimpleName(), String.format("Drag from %f:%f",
                position.latitude, position.longitude));
    }

}

1 个答案:

答案 0 :(得分:0)

每当模型发生变化并且您想要更新信息窗口时,您需要致电showInfoWindow

arg0.setTitle("HAHAH");
arg0.showInfoWindow();