web-dev-qa-db-fra.com

adaptateur de fenêtre d'informations personnalisé avec des données personnalisées dans map v2

Je veux créer un adaptateur de fenêtre d'informations personnalisé dans la carte v2 dans Android comme ci-dessous.

enter image description here

J'ai vu le lien ci-dessous mais je n'en ai pas plus.

1 , 2, ,

ci-dessous est mon fichier de mise en page de contenu.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="vertical" 
>
    <ImageView
        Android:id="@+id/infocontent_iv_image"
        Android:layout_width="150dp"
        Android:layout_height="150dp"
        Android:layout_alignParentTop="true" 
    />
    <RelativeLayout
        Android:id="@+id/infocontent_rl_middle"
        Android:layout_below="@id/infocontent_iv_image"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content" 
        Android:layout_margin="5dp"
    >

    </RelativeLayout>
    <TextView
        Android:id="@+id/infocontent_tv_name"
        Android:layout_below="@id/infocontent_rl_middle"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:textStyle="bold" 
        Android:layout_margin="5dp"
    />
    <TextView
        Android:id="@+id/infocontent_tv_type"
        Android:layout_below="@id/infocontent_tv_name"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:textColor="#CCCCCC"
        Android:layout_margin="5dp"
    />
    <TextView
        Android:id="@+id/infocontent_tv_desc"
        Android:layout_below="@id/infocontent_tv_type"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_margin="5dp"
    />
    <TextView
        Android:id="@+id/infocontent_tv_addr"
        Android:layout_below="@id/infocontent_tv_desc"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_margin="5dp"
    />
</RelativeLayout>

Donc, si quelqu'un m'aide, comment puis-je définir des données pour toutes les vues dans l'adaptateur infowindow?

41
Girish Bhutiya

J'ai ouvert le dialogue onMarker() cliquez. et setContentView(my layout).

J'ai utiliser le code ci-dessous.

@Override
    public boolean onMarkerClick(Marker arg0) {
        if(arg0.getSnippet() == null){
            mMap.moveCamera(CameraUpdateFactory.zoomIn());
            return true;
        }
        //arg0.showInfoWindow();
        final DataClass data = myMapData.get(arg0);
        final Dialog d = new Dialog(MainActivity.this);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //d.setTitle("Select");
        d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        d.setContentView(R.layout.info_content);
        ivPhoto = (ImageView)d.findViewById(R.id.infocontent_iv_image);
        AddImageOnWindow executeDownload = new AddImageOnWindow();
        final LatLng l = arg0.getPosition();
        executeDownload.execute(l);
        TextView tvName = (TextView)d.findViewById(R.id.infocontent_tv_name);
        tvName.setText(data.getPlaceName());

        TextView tvType = (TextView)d.findViewById(R.id.infocontent_tv_type);
        tvType.setText("("+data.getPlaceType()+")");

        TextView tvDesc = (TextView)d.findViewById(R.id.infocontent_tv_desc);
        tvDesc.setText(data.getPlaceDesc());

        TextView tvAddr = (TextView)d.findViewById(R.id.infocontent_tv_addr);
        tvAddr.setText(Html.fromHtml(data.getPlaceAddr()));

d.show();
return true;
19
Girish Bhutiya

Essaye ça

windowlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:orientation="vertical">

    <TextView
        Android:id="@+id/tv_lat"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content" />

    <TextView
        Android:id="@+id/tv_lng"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.Java

public class MainActivity extends FragmentActivity {


GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting reference to the SupportMapFragment of activity_main.xml
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    googleMap = mapFragment.getMap();

    // Setting a custom info window adapter for the google map
    googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        // Use default InfoWindow frame
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        // Defines the contents of the InfoWindow
        @Override
        public View getInfoContents(Marker arg0) {

            // Getting view from the layout file info_window_layout
            View v = getLayoutInflater().inflate(R.layout.windowlayout, null);

            // Getting the position from the marker
            LatLng latLng = arg0.getPosition();

            // Getting reference to the TextView to set latitude
            TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);

            // Getting reference to the TextView to set longitude
            TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);

            // Setting the latitude
            tvLat.setText("Latitude:" + latLng.latitude);

            // Setting the longitude
            tvLng.setText("Longitude:"+ latLng.longitude);

            // Returning the view containing InfoWindow contents
            return v;

        }
    });

    // Adding and showing marker while touching the GoogleMap
    googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {
            // Clears any existing markers from the GoogleMap
            googleMap.clear();

            // Creating an instance of MarkerOptions to set position
            MarkerOptions markerOptions = new MarkerOptions();

            // Setting position on the MarkerOptions
            markerOptions.position(arg0);

            // Animating to the currently touched position
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0));

            // Adding marker on the GoogleMap
            Marker marker = googleMap.addMarker(markerOptions);

            // Showing InfoWindow on the GoogleMap
            marker.showInfoWindow();

        }
    });

}
}

De cette façon, vous pouvez créer une mise en page personnalisée et réaliser ce que vous souhaitez.

Faites-moi savoir si cela vous aide.

69
moDev