1

對於我想使用CSV文件添加標記的項目。在信息窗口中的內容是基於一個CSV文件是這樣的:更改基於變量的Google地圖標記信息窗口中的圖像

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

在這一點上我可以添加標記到地圖並添加標題和描述信息窗口,但我不能設法添加一個基於CSV文件的圖像。圖像的名稱與id相同。所以,如果標題是m_1,我想要將名稱爲m_1的圖像添加到信息窗口。

我雖然我可以重寫圖像的現有源,但這不工作,因爲應用程序崩潰時,我推動應用程序中的標記之一。該代碼如下:

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(); 
     } 
    } 


} 

信息窗口的XML格式如下:

<?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> 

補充這一點我敢無能做什麼。我也嘗試在信息窗口中創建一個新的圖像視圖,但這也不起作用。

我在日誌貓得到的錯誤是:

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) 
+0

難道您發佈logcat的錯誤? – antonio

+0

@antonio我添加了錯誤 –

+0

在mMap.addMarker(new MarkerOptions()):ImageView img =(ImageView)findViewById(R.id.map_image); img.setImageResource(R.drawable.m_1)之前刪除這兩行。 ;' – antonio

回答

2

在您getInfoWindow方法代替

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

與此:

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

謝謝你的幫助!我不知道問題是我調用了默認的佈局文件而不是信息窗口xml文件 –

相關問題