简体   繁体   中英

how to add different images in different information window for different markers in google maps api v2 for android

I have made lot of markers now I want each information window of marker must be unique I have written xml for information window but its not working accordingly every time a single image shows up.

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

    // Use default InfoWindow frame

    public View getInfoWindow(Marker arg0) {
        return null;
    }

    // Defines the contents of the InfoWindow

    public View getInfoContents(Marker marker) {

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

        // Getting the position from the marker
        LatLng latLng = marker.getPosition();
        Double latitude=latLng.latitude;
        Double langitude=latLng.longitude;

        String lat = String.format("%.6f", latitude);
        String lng = String.format("%.6f", langitude);

        Toast.makeText(getApplicationContext(), lat+"----"+lng,Toast.LENGTH_SHORT).show();
        // Getting reference to the TextView to set longitude
    TextView tvLng = (TextView) v.findViewById(R.id.tv1);
    ImageView icon = (ImageView) v.findViewById(R.id.icons);

    if(lat.equals("73.057130") && lng.equals("33.721140"))
    {
        icon.setImageResource(R.drawable.hbl_icon);

    }
    else
    {

        icon.setImageResource(R.drawable.alflah_icon);
    }
        // Returning the view containing InfoWindow contents
        return v;

    }

});

in myMap.setInfoWindowAdapter the toast is showing the same latitude but the information window appearing is not showing the image in if condition and always showing a image in else condition.everything works fine but the image is not showing proper kindly help. my xml is :

<?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="horizontal" >

    <ImageView
        android:id="@+id/icons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/tv1"
        android:text="Get Directions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" />

</LinearLayout>

You should not use LatLng to identify Marker because of this bug .

You may use title / snippet instead and make ifs on that value or have a Map<Marker, YourModel> of all markers and keep icon resource id in that model.

Or you can use Android Maps Extensions , which adds functions like Marker.setData(Object) and Object Marker.getData() and keep your model with icon resource id close to marker.

    for(int i = 0 ; i < arraylist.size();i++){
        addMarker(mapview,arraylistLat.get(i),arraylistLong.get(i),title.get(i),arraysnippet.get(i),imageid[i]);
    }

private void addMarker(GoogleMap map, double lat, double lon,
            String string, String string2,int imgid) {
        map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                .title(string).snippet(string2).icon(BitmapDescriptorFactory.fromResource(imgid));
    }

Here imgid is the id of drawable resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM