4

I'm trying to setup custom image annotations in the Android SDK and I'm no able to. If I create the annotation with a default image with the code:

annotation.setAnnotationType(SKAnnotation.SK_ANNOTATION_TYPE_GREEN);

The annotation is displayed. But when I set my custom image with the code:

annotation.setImagePath(getActivity().getFilesDir() + "/" + data.getMapImagePath());
annotation.setImageSize(64);

The annotation is not displayed. The variables in the image path resolve to (for example): "/data/data/com.kolobee.mini/files/stings_chueca.me9d_map.png".

This images are generated dynamically by the app by creating a png file from a Bitmap with the code:

FileOutputStream fos = context.openFileOutput(path, Context.MODE_PRIVATE);
bitmap.compress(CompressFormat.PNG, 75, fos);
fos.close();

Why are annotations not being displayed?

eliocs
  • 18,511
  • 7
  • 40
  • 52
  • Custom images are not working on my side too. This is the image I'm using: cl.ly/image/0l0T2n1B3O07 (Photoshop generated, 64x64, 24-bit transparent PNG) SKAnnotation *annotation = [SKAnnotation annotation]; // set unique ID // set location annotation.imagePath = [[NSBundle mainBundle] pathForResource:@"purpleAlert" ofType:@"png"]; [self.mapView addAnnotation:annotation]; No luck. Nothing is displayed. – backslash-f Jul 07 '14 at 00:35

1 Answers1

0

In 2.1.0 we've added support for using images from the resources bundle & extended the examples for full path images - here's the updated prepareAnnotations code:

 /**
 * Draws annotations on map
 */
private void prepareAnnotations() {

    // get the annotation object
    SKAnnotation annotation1 = new SKAnnotation();
    // set unique id used for rendering the annotation
    annotation1.setUniqueID(10);
    // set annotation location
    annotation1.setLocation(new SKCoordinate(-122.4200, 37.7765));
    // set minimum zoom level at which the annotation should be visible
    annotation1.setMininumZoomLevel(5);
    // set the annotation's type
    annotation1.setAnnotationType(SKAnnotation.SK_ANNOTATION_TYPE_RED);
    // render annotation on map
    mapView.addAnnotation(annotation1);

    SKAnnotation annotation2 = new SKAnnotation();
    annotation2.setUniqueID(11);
    annotation2.setLocation(new SKCoordinate(-122.410338, 37.769193));
    annotation2.setMininumZoomLevel(5);
    annotation2.setAnnotationType(SKAnnotation.SK_ANNOTATION_TYPE_GREEN);
    mapView.addAnnotation(annotation2);

    SKAnnotation annotation3 = new SKAnnotation();
    annotation3.setUniqueID(12);
    annotation3.setLocation(new SKCoordinate(-122.430337, 37.779776));
    annotation3.setMininumZoomLevel(5);
    annotation3.setAnnotationType(SKAnnotation.SK_ANNOTATION_TYPE_BLUE);
    mapView.addAnnotation(annotation3);

    // annotation drawn with drawable resource
    SKAnnotation annotation4 = new SKAnnotation();
    annotation4.setUniqueID(13);
    annotation4.setLocation(new SKCoordinate(-122.425, 37.774));
    annotation4.setMininumZoomLevel(5);
    SKAnnotationView annotationView = new SKAnnotationView();
    // set the drawable resource to be rendered as annotation
    annotationView.setDrawableResourceId(R.drawable.dot_full);
    // set the size of the annotation (this value must be a power of 2)
    annotationView.setProperSize(16);
    annotation4.setAnnotationView(annotationView);
    mapView.addAnnotation(annotation4);

    // annotation drawn with image from a local file
    SKAnnotation annotation5 = new SKAnnotation();
    annotation5.setUniqueID(14);
    annotation5.setLocation(new SKCoordinate(-122.417, 37.772));
    annotation5.setMininumZoomLevel(5);

    // set path to an image whose dimensions are powers of 2
    // image is selected according to screen density
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    if (metrics.densityDpi < DisplayMetrics.DENSITY_HIGH) {
        annotation5.setImagePath(app.getMapResourcesDirPath() + "images/dot_blue_medium.png");
    } else {
        annotation5.setImagePath(app.getMapResourcesDirPath() + "images/dot_blue_high.png");
    }
    annotation5.setImageSize(40);
    mapView.addAnnotation(annotation5);

    selectedAnnotation = annotation1;
    // set map zoom level
    mapView.setZoom(14);
    // center map on a position
    mapView.centerMapOnPosition(new SKCoordinate(-122.4200, 37.7765));
    updatePopupPosition();
}
Ando
  • 11,199
  • 2
  • 30
  • 46