Search code examples
androidgoogle-mapsgoogle-maps-android-api-2infowindow

change image in the info window of google maps marker based on variable


For a project i want to use a CSV file to add markers. The content in the info window is based on a CSV file like this:

x_coordinate          y_coordinate         title       description
52.3494703659177      4.63031105906557     m_1         beautifull
52.3494703659177      4.63031105906557     m_2         Awfull

At this point i can add the markers to the map and add the title and description to the info window but i can't manage to add a image based on the CSV file. The name of the image is the same as the id. So, if the title is m_1 i want to add image with the name m_1 to the info window.

I though i could override the existing source of the image but this doens't work due the fact that the app crash when i push one of the markers in the app. The code is as following:

public void onMapReady(GoogleMap googlemap) {
    mMap = googlemap;

    if (mMap != null) {
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.infow_window, null);

                TextView tvHaarlem = (TextView) v.findViewById(R.id.tvHaarlem);
                TextView tvSnippet = (TextView) v.findViewById(R.id.tvNoord_Holland);
                String image_id = new String(marker.getTitle());
                int resID = getResources().getIdentifier(image_id, "drawable", "package.name");
                ImageView img= (ImageView) findViewById(R.id.map_image);
                img.setImageResource(R.drawable.m_1);

                LatLng ll = marker.getPosition();
                tvHaarlem.setText(marker.getTitle());
                tvSnippet.setText(marker.getSnippet());

                return v;
            }

            @Override
            public View getInfoContents(Marker marker) {
                return null;
            }

        });

        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Intent intent = new Intent(maps.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }



   InputStream instream = getResources().openRawResource(R.raw.points_in_map);
    InputStreamReader inputreader = new InputStreamReader(instream);
    BufferedReader reader = new BufferedReader(inputreader);
    List<LatLng> latLngList = new ArrayList<LatLng>();
    String line = "";

 try {
            while ((line = reader.readLine()) != null) // Read until end of file
            {
                double lat = Double.parseDouble(line.split(",")[1]);
                double lon = Double.parseDouble(line.split(",")[2]);
                String title_marker = new String(line.split(",")[3]);
                String description_marker = new String(line.split(",")[4]);
                LatLng pos = new LatLng(lat, lon);

                //coordinates too map
                mMap.addMarker(new MarkerOptions()
                        .title(String.valueOf(title_marker))
                        .snippet(String.valueOf(description_marker))
                        .position(pos));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

The xml of the info window is as following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    >

    <ImageView
        android:id="@+id/map_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_welgelegen"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:onClick="sendmessage">

        <TextView
            android:id="@+id/tvHaarlem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvNoord_Holland"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"/>

    </LinearLayout>

</LinearLayout>

add this point i m pretty clueless what to do. I also tried to create a new image view in the info window but this wouldn't work either.

the error i get in log cat is:

03-17 13:48:58.097 9689-9689/com.diovae.berend.layout_legend E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.diovae.berend.layout_legend, PID: 9689
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                                                                                   at com.diovae.berend.layout_legend.maps$1.getInfoWindow(maps.java:63)
                                                                                   at com.google.android.gms.maps.GoogleMap$7.zzh(Unknown Source)
                                                                                   at com.google.android.gms.maps.internal.zzd$zza.onTransact(Unknown Source)
                                                                                   at android.os.Binder.transact(Binder.java:499)
                                                                                   at com.google.android.gms.maps.internal.p.a(:com.google.android.gms.DynamiteModulesB:94)
                                                                                   at com.google.maps.api.android.lib6.impl.by.a(:com.google.android.gms.DynamiteModulesB:89)
                                                                                   at com.google.maps.api.android.lib6.impl.by.a(:com.google.android.gms.DynamiteModulesB:124)
                                                                                   at com.google.maps.api.android.lib6.gmm6.api.e.a(:com.google.android.gms.DynamiteModulesB:188)
                                                                                   at com.google.maps.api.android.lib6.gmm6.api.g.c(:com.google.android.gms.DynamiteModulesB:200)
                                                                                   at com.google.maps.api.android.lib6.impl.db.g(:com.google.android.gms.DynamiteModulesB:23225)
                                                                                   at com.google.maps.api.android.lib6.impl.dd.b(:com.google.android.gms.DynamiteModulesB:304)
                                                                                   at com.google.maps.api.android.lib6.gmm6.api.e.a(:com.google.android.gms.DynamiteModulesB:242)
                                                                                   at com.google.maps.api.android.lib6.gmm6.vector.m.a(:com.google.android.gms.DynamiteModulesB:4070)
                                                                                   at com.google.maps.api.android.lib6.gmm6.vector.af.c(:com.google.android.gms.DynamiteModulesB:611)
                                                                                   at com.google.maps.api.android.lib6.gmm6.vector.df.onSingleTapConfirmed(:com.google.android.gms.DynamiteModulesB:236)
                                                                                   at com.google.maps.api.android.lib6.impl.gesture.g.onSingleTapConfirmed(:com.google.android.gms.DynamiteModulesB:189)
                                                                                   at com.google.maps.api.android.lib6.impl.gesture.i.handleMessage(:com.google.android.gms.DynamiteModulesB:132)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

Solution

  • On your getInfoWindow method replace

    ImageView img= (ImageView) findViewById(R.id.map_image);
    

    with this:

    ImageView img= (ImageView) v.findViewById(R.id.map_image);