0

I have saved all the relevant information needed to reload the markers once the app has restarted. Now the problem is it just reloads the title and snippet but not the image associated with that marker:

So in my database this is what is saved: (after the = its the value saved and in () the column name

Column 1 = 1 (id)
column 2 = xx.xxx (lng)
column 3 = xx.xxx (lat)
column 4 = 9.0 (zoom)
column 5 = test (title)
column 6 = marker (snippet)
column 7 = m12 (marker id)
column 8 = /storage/emulated/0/Images/myimage_yyyymmdd_hhmmss.jpg (imagepath)

So as you can see all the information is saved to reload the marker with the title, snippet, zoom level as well as the image and latlng point.

But however when restarting the map the marker is displayed on the correct location with the title and the snippet, but the image isn't displayed (the image is displayed in the CustomInfoWindow

So here is the code to save the values and to retrieve them:

contentValues.put(LocationsDB.FIELD_LAT, point.latitude);
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);
contentValues.put(LocationsDB.FIELD_TITLE, title.getText().toString());
contentValues.put(LocationsDB.FIELD_SNIPPET, snippet.getText().toString());
contentValues.put(LocationsDB.FIELD_IMAGE, markerId);
contentValues.put(LocationsDB.FIELD_FILEPATH, image.getAbsolutePath());
.....
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
int locationCount = 0;
double lat=0;
double lng=0;
float zoom=0;
String title = null;
String snippet = null;
String id = null;
String filep = null;
locationCount = arg1.getCount();
arg1.moveToFirst();

for(int i=0;i<locationCount;i++){
  lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
  lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
  zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
  title = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_TITLE));
  snippet = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_SNIPPET));
  id = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
  filep = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));
  thePoint = new LatLng(lat, lng);
  drawMarker(thePoint, title, snippet, id, filep);
  View v  = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
  ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
  Bitmap myImage = BitmapFactory.decodeFile(filep);
  markerIcon.setImageBitmap(myImage);
  arg1.moveToNext();

  }

  if(locationCount>0){
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
       }
        }

And for when drawing the marker on the map:

private void drawMarker(LatLng point, String title, String snippet, String id, String filep) {
Marker marker = googleMap.addMarker(new MarkerOptions()
    .title(title)
    .snippet(snippet)
    .position(thePoint));
    markerId = marker.getId();
}

So what am i doing wrong here and do i need to somehow when the marker gets drawing to say load id and image path?

Could someone please shed some light on this for me?

Thanks

1 Answers1

0

Try those changes:

    for(int i=0;i<locationCount;i++){
      lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
      lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
      zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
      title = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_TITLE));
      snippet = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_SNIPPET));
      id = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
      filep = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));
      thePoint = new LatLng(lat, lng);
      BitmapDescriptor yourImage = BitmapDescriptorFactory.fromPath(filep);
      /*
      * Use BitmapDescriptor.fromPath(FILE_PATH) to create a bitmap descriptor from an absolute file path.
      * Or BitmapDescriptor.fromFile(NAME), if you have the name of an image file located in the internal storage
      */
      drawMarker(thePoint, title, snippet, id, filep, yourImage);
      arg1.moveToNext();

   }

and in your method:

private void drawMarker(LatLng point, String title, String snippet, String id, String filep, BitmapDescriptor image) {
Marker marker = googleMap.addMarker(new MarkerOptions()
    .title(title)
    .snippet(snippet)
    .icon(image)
    .position(thePoint));
    markerId = marker.getId();
}
Rami
  • 7,879
  • 12
  • 36
  • 66
  • Cool Thanks, it is displaying the image but massively! but is there a way of using the default marker and when tapping on the marker to display the image then? –  Jan 13 '15 at 09:41
  • For your massive displaying, you need to change the size of your images (eg: from 512x512 to 72x72), coz by default the marker *ImageView* is *WrapContent* so it take the same size of your .png file. I haven't understand your question about default marker. – Rami Jan 13 '15 at 10:05
  • Okay, thanks will resize them. Basically I have a custominfo window that should display the image, so when the user taps on the marker the image is displayed in the custominfowindow. So instead of having the image as a marker, have a defaut marker with the image in the custominfowindow. –  Jan 13 '15 at 11:08
  • If you want to put the image in your customInfoWindow, you need to create a custom *InfoWindowAdapter* (and set it to your map by: GoogleMap.setInfoWindowAdapter(...) ). And in *getInfoContents(Marker marker)* method of this adapter, you can set your image. Take a look at this answer : http://stackoverflow.com/a/15091202/4224337 – Rami Jan 13 '15 at 11:20