在我的應用程式中,我可以選擇在地圖上的特定點上“長按”,然后它會顯示一個自定義對話框視窗,您可以在其中輸入自定義“標題”和“片段”,然后拍照然后保存。然后,這會將標記添加到地圖。
現在這是我有點卡住的地方。我可以讓它顯示每個標記的標題和片段,但不顯示為該特定標記拍攝的影像。
我曾嘗試將標記保存到 HashMap
private HashMap<Marker, String> markerImageID = new HashMap<Marker, String>();
然后將其添加到drawMarker方法中:
private void drawMarker(LatLng location, String infoTitle, String infoSnippet, String img) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(location);
mMap.addMarker(new MarkerOptions()
.title(infoTitle)
.snippet(infoSnippet)
.position(location)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));
markerImageID.put(marker, img);
}
然后當點擊地圖示記時,我使用以下代碼:
@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
LatLng latLng = marker.getPosition();
ImageView imgViewMarker = (ImageView) v.findViewById(R.id.imageViewFinal);
TextView tvLat = (TextView) v.findViewById(R.id.txtMarkerTitle);
TextView tvLng = (TextView) v.findViewById(R.id.txtMarkerSnippet);
String filep = markerImageID.get(marker.getId());
if (filep != null) {
Bitmap myBitmap = BitmapFactory.decodeFile(img);
imgViewMarker.setImageBitmap(myBitmap);
}
tvLat.setText(marker.getTitle());
tvLng.setText(marker.getId());
return v;
}
但不幸的是,這不起作用,因為該特定標記沒有顯示影像。其他資訊如片段、標題和影像使用 SQLite 資料庫保存:
ContentValues contentValues = new ContentValues();
contentValues.put(LocationsDB.FIELD_LAT, point.latitude);
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_ZOOM, mMap.getCameraPosition().zoom);
contentValues.put(LocationsDB.FIELD_TITLE, txtTitle.getText().toString());
contentValues.put(LocationsDB.FIELD_SNIPPET, txtSnippet.getText().toString());
contentValues.put(LocationsDB.FIELD_IMAGE_PATH, image.getAbsolutePath());
但是,當我將影像添加為這樣時:
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
LatLng latLng = marker.getPosition();
ImageView imgViewMarker = (ImageView) v.findViewById(R.id.imageViewFinal);
TextView tvLat = (TextView) v.findViewById(R.id.txtMarkerTitle);
TextView tvLng = (TextView) v.findViewById(R.id.txtMarkerSnippet);
Bitmap myBitmap = BitmapFactory.decodeFile(img);
imgViewMarker.setImageBitmap(myBitmap);
tvLat.setText(marker.getTitle());
tvLng.setText(marker.getId());
return v;
然后它在 中顯示影像ImageView,但總是最后拍攝的影像,然后它為添加的所有其他標記顯示相同的影像。
然后我想到使用 HashMap 來保存標記的 id,然后以這種方式顯示影像,但它不起作用。
希望有人能幫我解決這個問題?
謝謝
uj5u.com熱心網友回復:
不是存盤包含標記影像的哈希圖,而是在標記本身上設定標簽怎么樣?請參閱:https : //developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/model/Marker?hl=en
與標記關聯的物件。例如,物件可以包含有關標記代表什么的資料。這比存盤單獨的 Map<Marker, Object> 更容易。再舉一個例子,您可以將一個字串 ID 與資料集中的 ID 相關聯。Android 版 Google Maps SDK 既不讀取也不寫入此屬性。
您可以將字串設定為標簽,或者,您可以創建一個包裝所有標記資料的模型,并將該模型的一個實體設定為標簽。
至于影像未顯示,您是否在日志中看到了任何錯誤訊息?
uj5u.com熱心網友回復:
我認為你有一個小錯誤drawMarker,你沒有使用結果addMarker是添加的Marker物件,所以你將 與img之前分配的一些相關聯marker:
private void drawMarker(LatLng location, String infoTitle, String infoSnippet, String img) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(location);
// HERE: addMarker returns a Marker object and you never assign marker to it.
// So I added the assignment
marker = mMap.addMarker(new MarkerOptions()
.title(infoTitle)
.snippet(infoSnippet)
.position(location)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));
// So in your original code the marker was never changing to the recently
// added marker when you then associate it to the image.
markerImageID.put(marker, img);
}
從代碼中不清楚該marker變數是如何在其他地方使用的(它不是本地的),因此如有必要,請使用本地變數來接收標記并在放置中使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355565.html
