Reputation: 11
I'm developing an Android application using osmdroid and osmbonus pack libraries which allows the user to drag and drop icons to the map from a menu. The application is intended to allow the user to "save the map", so i'm trying to manage how to save all the dragged markers in a kml document, and than loading it when necessary.
I've tried following the 13.3 tutorial https://github.com/MKergall/osmbonuspack/wiki/Tutorial_4 , but i still can't figure it out, so i'm probably missing something.
I created a class for the insert of the markers in the map, with this method:
public void insertIcon(int image) throws IOException {
marker = new Marker(map);
point = new GeoPoint(currentLocation);
marker.setPosition(point);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(marker);
map.invalidate();
applyDraggableListener(marker);
marker.setIcon(activity.getDrawable(image));
}
And then once the marker is dragged in a certain position, i add it to the kml structure:
...
public void onMarkerDragEnd(Marker marker) {
GeoPoint geopoint = marker.getPosition();
//poiMarker.setDraggable(false);
Utilities.kmlDocument.mKmlRoot.addOverlay(marker, Utilities.kmlDocument);
}
...
At this point, i don't know how to assign to that specific marker its specific style, which is basically just the icon.
I've made several tests inside the MyKmlStyler, but the main point of the problem is in fact that i don't know how distinguish between each kmlPlacemark.
Inside the method onPoint
of MyKmlStyler
i put a println and obviously all the placemarks have the same style.
@Override
public void onPoint(Marker marker, KmlPlacemark kmlPlacemark, KmlPoint kmlPoint) {
System.out.println(kmlPlacemark);
}
The only thing i'm able to do is giving a unique style to all the markers dragged.
I've already tried with putStyle
and the other ways described in the tutorial, but as i've already said, most likely i'm missing something.
My goal is to give each kmlPlacemark a different style according to what the real icon of the marker the user dragged is, all inside the same kmlDocument.
Edit
I thought i managed to find a solution this way:
public void onMarkerDragEnd(Marker marker) {
/*GeoPoint geopoint = marker.getPosition();
poiMarker.setDraggable(false);
*/
KmlDocument mKmlDocument = new KmlDocument();
KmlPlacemark p = new KmlPlacemark(marker);
Style s = buildStyle();
p.mStyle = mKmlDocument.addStyle(s);
mKmlDocument.mKmlRoot.add(p);
mKmlOverlay = (FolderOverlay) mKmlDocument.mKmlRoot.buildOverlay(map, null, null, mKmlDocument);
Utilities.kmlDocument.mKmlRoot.addOverlay(mKmlOverlay, Utilities.kmlDocument);
}
private Style buildStyle(){
Drawable defaultMarker = AppCompatResources.getDrawable(activity, R.drawable.person);
Bitmap defaultBitmap = ((BitmapDrawable) defaultMarker).getBitmap();
return new Style(defaultBitmap, 0x901010AA, 3.0f, 0x20AA1010);
}
but still, no style is applied to the markers, so the icon shown is the default one, not the chosen one.
Note: Utilities.kmlDocument
is where i store all the map markers so it is different from mKmlDocument
Upvotes: 1
Views: 226
Reputation: 3450
Basically, your root cause issue if that you try to rely on markers to build your KML document. You should handle your own "model", and from that model, build markers as needed, and build KML document when needed.
Your "model" can probably be something as simple as an arraylist of objects with: a GeoPosition, and a "type". Depending on this type, you will be able to create markers with various icons, and KmlPlacemarks with various styles.
Upvotes: 0