tags:

views:

434

answers:

1

I am building a MapView and I want my custom overlay items to display the name of the location they are marking when the user taps them, like the Android Maps app.

I setup the onTap listener and the floating TextView to hold the location name. I still need to set it up so that it redraws the label when the user moves the map, etc.

Anyway, I am wondering if I am reinventing the wheel here. Is there a built-in method I am unaware of? I would think that most implementations of MapView have labels.

For reference, my implementation so far:

in map xml:

<LinearLayout android:id="@+id/mapBubbleWrap"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true">
    <TextView android:id="@+id/mapBubble" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:visibility="gone" 
        android:background="#ffffff" 
        android:textColor="#ff0000"/>
</LinearLayout>

in my extended ItemizedOverlay:

public boolean onTap(int index) {

    this.setFocus( mOverlays.get(index) );      
    return true;
}

in my Activity onFocus:

public void onFocusChanged( ItemizedOverlay overlay, OverlayItem item ) {
    if( item != null) {
        mapBubble.setText(item.getTitle());
        Point newPoint = mapView.getProjection().toPixels(item.getPoint(), null);
        mapBubbleWrap.setPadding(newPoint.x, newPoint.y-10, 0, 0);
        mapBubble.setVisibility(View.VISIBLE);
    }
}
+1  A: 

I would think that most implementations of MapView have labels.

Possibly, but there is nothing built into the Google Map add-on to support them, beyond what you are doing. Though I'm not completely following what the whole focus thing is about. If I want a popup panel when the user taps, I just display a popup panel when the user taps -- see this project for an example.

CommonsWare