Osmdroid Bonuspack - MyLocationNewOverlay - java

I currently have a couple of features that are causing a few problems that where originally working but after changing some things around are now producing errors. Using Android Studio which allowed me to look at previous versions of the code but to no avail.
Anyway I have a MyLocationNewOverlay declared globally like so:
MyLocationNewOverlay location_overlay;
Which gets initiated when the user navigates to the activity with the map:
map = (MapView) findViewByID(R.id.map);
map.setVisibility(MapView.VISIBLE);
<..some working code that sets the tile source and the center..>
location_overlay = new MyLocationNewOverlay(getApplicationContext(), map);
location_overlay.enableMyLocation();
location_overlay.setDrawAccuracyEnabled(true);
map.getOverlays().add(location_overlay);
map.invalidate();
When it was working this code displayed a little human marker with the accuracy circle around it but now it doesn't even though it doesn't produce any errors. Iv'e tried the now decrepit MyLocationOverlay which didn't work either.
The second issue lies within an 'onClick' method on a button that supposed to focus the map on the users current location, this also used to work.
public void onBtnFocusOnMe(View view){
GeoPoint gp = new GeoPoint(location_overlay.getMyLocation());
if(gp != null){
mapController.animateTo(gp);
mapController.zoomTo(16);
}
}
Which produces a null pointer error on GeoPoint gp = new GeoPoint(location_overlay.getMyLocation());

How I normally overlay some items is like this, it is not directly your solution but you can maybe extract something useful from here:
public void showStartGoalMarkers(GeoPoint start, GeoPoint goal) {
List<OverlayItem> mStartGoalItems = new ArrayList<>();
OverlayItem startItem = new OverlayItem("", "", start);
Drawable newMarker = mMapView.getContext().getResources().getDrawable(R.drawable.ic_start);
startItem.setMarker(newMarker);
mStartGoalItems.add(startItem);
OverlayItem goalItem = new OverlayItem("", "", goal);
Drawable newMarker2 = mMapView.getContext().getResources().getDrawable(R.drawable.ic_end);
goalItem.setMarker(newMarker2);
mStartGoalItems.add(goalItem);
mMapView.getOverlays().add(new ItemizedIconOverlay<OverlayItem>(mStartGoalItems, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
#Override
public boolean onItemSingleTapUp(int index, OverlayItem item) {
return false;
}
#Override
public boolean onItemLongPress(int index, OverlayItem item) {
return false;
}
}, mMapView.getResourceProxy()));
}
and in the end you invalidate the map view. Hope it helps.
EDIT: the code for marking the current location and which also updates the current position when a new location is passed:
private void markMyLocation(Location location) {
mOverlayItems.add(0, new OverlayItem("", "", new GeoPoint(location)));
if (mMyLocationOverlay == null) {
mMyLocationOverlay = new MyLocationOverlay(mOverlayItems, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
#Override
public boolean onItemSingleTapUp(int index, OverlayItem item) {
IMapController mapController = mMapView.getController();
mapController.setCenter(item.getPoint());
mapController.setZoom(mMapView.getMaxZoomLevel());
return true;
}
#Override
public boolean onItemLongPress(int index, OverlayItem item) {
return false;
}
}, mMapView.getResourceProxy());
mMapView.getOverlays().add(mMyLocationOverlay);
mMapView.getController().setZoom(16);
} else {
IMapController mapController = mMapView.getController();
mapController.setCenter(mOverlayItems.get(0).getPoint());
mMapView.invalidate();
}
}
The MyLocationOverlay class:
public class MyLocationOverlay extends ItemizedIconOverlay<OverlayItem> {
List<OverlayItem> mMyLocation;
int mResourceId;
public MyLocationOverlay(List<OverlayItem> pList,
OnItemGestureListener<OverlayItem> pOnItemGestureListener,
ResourceProxy pResourceProxy) {
super(pList, pOnItemGestureListener, pResourceProxy);
this.mMyLocation = pList;
this.mResourceId = R.drawable.my_location;
}
#Override
public void draw(Canvas canvas, MapView mapview, boolean arg2) {
super.draw(canvas, mapview, true);
if (!mMyLocation.isEmpty()) {
IGeoPoint geoPointLocation = mMyLocation.get(0).getPoint();
Point out = new Point();
mapview.getProjection().toPixels(geoPointLocation, out);
Bitmap bm = BitmapFactory.decodeResource(mapview.getResources(),
mResourceId);
canvas.drawBitmap(bm,
out.x - bm.getWidth() / 2, //shift the bitmap center
out.y - bm.getHeight() / 2, //shift the bitmap center
null);
}
}
#Override
public boolean onSingleTapUp(MotionEvent event, MapView mapView) {
// TODO Auto-generated method stub
//return super.onSingleTapUp(event, mapView);
return true;
}
Basically what I do is I overwrite the single item in the ArrayList mOverlayItems when the method is called and invalidate the map.

Related

Display GroundOverlay only when building is entirely within cameraView

As stated in the title, essentially I only want to display the GroundOverlay when the camera is in view of the entire building. How would I accomplish this within the onCameraMove() method? As of now the overlay appears even when part of the building is within the camera view.
#Override
public void onCameraMove(){
LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
private static final LatLng Building1 = new LatLng(54.69726685890506,-2.7379201682812226);
if(mMap.getCameraPosition().zoom > 17){
if (bounds.contains(Building1)) {
displayOverlay();
}
}
It seems that Building1 should be a list of the boundary points of the building, and you should check in a loop that ALL of them are contained inbounds:
...
List<LatLng> buildingPoints = new ArrayList<>();
buildingPoints.add(new LatLng(...,...))
buildingPoints.add(new LatLng(...,...))
...
...
if(mMap.getCameraPosition().zoom > 17){
boolean allPointsVisible = true;
for (LatLng currBuildingPoint: buildingPoints) {
if (!bounds.contains(currBuildingPoint)) {
allPointsVisible = false;
break;
}
}
if (allPointsVisible) {
displayOverlay();
}
}
...

Add marker to OSMdroid 5.5 map

I want to add markers to my OSMdroid map. I am using OSMdroid version 5.5. The official tutorial suggests the following code:
//your items
ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
items.add(new OverlayItem("Title", "Description", new GeoPoint(0.0d,0.0d))); // Lat/Lon decimal degrees
//the overlay
ItemizedOverlayWithFocus<OverlayItem> mOverlay = new ItemizedOverlayWithFocus<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
#Override
public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
//do something
return true;
}
#Override
public boolean onItemLongPress(final int index, final OverlayItem item) {
return false;
}
}, mResourceProxy); // <----- where to get this object from?
mOverlay.setFocusItemsOnTap(true);
mMapView.getOverlays().add(mOverlay);
However, I don't know where to get the mResourceProxy object from. All websites I found about this topic (including OSMdroid's GitHub page) are making use of the DefaultResourceProxyImpl class, which is deprecated since version 5.2.
Does anyone know how to add marker versions >= 5.2?
Okay, so I found out how to use it. The ItemizedOverlayWithFocus doesn't require a ResourceProxy at all. So you can use one of the following constructors:
public ItemizedOverlayWithFocus(Context pContext, List<Item> aList, OnItemGestureListener<Item> aOnItemTapListener) { ... }
public ItemizedOverlayWithFocus(List<Item> aList, OnItemGestureListener<Item> aOnItemTapListener, Context pContext) { ... }
public ItemizedOverlayWithFocus(List<Item> aList, Drawable pMarker, Drawable pMarkerFocused, int pFocusedBackgroundColor, OnItemGestureListener<Item> aOnItemTapListener, Context pContext) { ... }
This is how I adjusted the code from my question to make it work:
//your items
ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
items.add(new OverlayItem("Title", "Description", new GeoPoint(0.0d,0.0d))); // Lat/Lon decimal degrees
//the overlay
ItemizedOverlayWithFocus<OverlayItem> mOverlay = new ItemizedOverlayWithFocus<OverlayItem>(
this, items, // <--------- added Context this as first parameter
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
#Override
public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
//do something
return true;
}
#Override
public boolean onItemLongPress(final int index, final OverlayItem item) {
return false;
}
}); // <----- removed the mResourceProxy parameter
mOverlay.setFocusItemsOnTap(true);
mMapView.getOverlays().add(mOverlay);

OSMDROID - longPressHelper only fires the first time when it is used to add a marker

I'm using osmdroid and have implemented a MapEventsReceiver, MapEventsOverlay and a LongPressHelper in order to add a new marker when the user holds down on the map. This works the first time and the first time only.
If I remove the addMarker(p); from my longPressHelper then it will fire every single time.
Has anyone got any idea why this is happening like this?
code:
mapviewInit - called in onCreate
private void mapviewInit() {
mapview = (MapView) findViewById(R.id.mapview);
mapview.setTileSource(TileSourceFactory.MAPNIK);
mapview.setBuiltInZoomControls(true);
mapview.setMultiTouchControls(true);
IMapController mapController = mapview.getController();
mapController.setZoom(16);
GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
mapController.setCenter(startPoint);
MapEventsReceiver meReceiver = new MapEventsReceiver() {
#Override
public boolean singleTapConfirmedHelper(GeoPoint p) {
return false;
}
#Override
public boolean longPressHelper(GeoPoint p) {
Toast toast = Toast.makeText(getApplicationContext(), "DEBUGDEBUGDEBUG", Toast.LENGTH_LONG);
toast.show();
addMarker(p);
return true;
}
};
addMarker - called by LongPressHelper
public void addMarker(GeoPoint geoPoint) {
Drawable dr = getResources().getDrawable(R.drawable.icn_crosshair_red);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 100, 100, true));
mapview.getOverlays().clear();
mapview.invalidate();
selectedPosMarker = new Marker(mapview);
selectedPosMarker.setPosition(geoPoint);
selectedPosMarker.setInfoWindow(null);
selectedPosMarker.setIcon(d);
selectedPosMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
mapview.getOverlays().add(selectedPosMarker);
mapview.invalidate();
}
I assume you add a MapEventsOverlay somewhere, in a part of your code you didn't provided.
The issue is that you remove it in addMarker! :
mapview.getOverlays().clear();
So of course it is not present to react to long press on the second time...
Don't call mapview.invalidate() twice in the same method, it's useless and time-consuming.

Marker click event in android map using osm

i am using following code for creating marker(bitmap),how to add click event for marker. i use graphhopper android for OSM map
startMarker = createMarker(startPoint, R.drawable.marker_departure); layers.add(startMarker);
add use createMarker method
public Marker createMarker(LatLong p, int resource) {
Drawable drawable = activity.getResources().getDrawable(resource);
Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
return new Marker(p, bitmap, 0, -bitmap.getHeight() / 2);
}
and how to add text near to marker
thanks in advance
Try with this, may be this will help full.
layers.setOnMarkerClickListener(new OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
//Your stuff
});
i use following code its works for me
MyMarker frommarker = newMyMarker(activity,newLatLong(fl.latitude,fl.longitude), AndroidGraphicFactory.convertToBitmap(activity.getResources().getDrawable(R.drawable.marker_departure)), 0, 0);
mapView.getLayerManager().getLayers().add(frommarker);
and mymarker class
public class MyMarker extends Marker {
private Context ctx;
public MyMarker(Context ctx, LatLong latLong, Bitmap bitmap, int horizontalOffset,
int verticalOffset) {
super(latLong, bitmap, horizontalOffset, verticalOffset);
this.ctx = ctx;
}
#Override
public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
if (this.contains(layerXY, tapXY)) {
}
}

Line appears in OverlayItem on Google Maps

I'm using an ItemizedOverlay on a Google Maps in an Android application. My extension of ItemizedOverlay is shown below.
The strange thing is I see a small line from about the 8 o'clock position to the 2 o'clock position in every occurrence of the overlay. The overlay is a png that is in my application resources (which obviously doesn't have the strange line). I've attached an example of the raw png overlay (R.drawable.green) here:
And here is what I see in the android app:
(You kind of have to look closely to see the gray line I'm talking about. It is under the green dots and under the little airplane icon.)
The overlay is added as follows:
ReportOverlay itemizedoverlay = new ReportOverlay(getResources().getDrawable(R.drawable.green),mContext);
GeoPoint point = new GeoPoint(pr.getLat(),pr.getLng());
OverlayItem overlayitem = new OverlayItem(point, pr.getReport(),pr.getReport());
itemizedoverlay.addOverlay(overlayitem);
Any idea where this mystery line in the overlay is coming from?!
public class ReportOverlay extends ItemizedOverlay<OverlayItem> {
protected ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public ReportOverlay(Drawable defaultMarker) {
super(boundCenter(defaultMarker));
}
public ReportOverlay(Drawable defaultMarker, Context context) {
super(boundCenter(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
#Override
public int size() {
return mOverlays.size();
}
}
As MH suggested, it was indeed the shadow that I was seeing. Turning it off fixed the issue.

Categories