I want to do a simple thing. I would like to mark a point of my current location.
At the moment I have osm map that display the screen with my current location, on top of this I can see my current position (longitude and latitude), but I don't know how to mark the current location.
I've not used OSMDroid a huge amount but you could mark your location using an ItemizedIconOverlay. This sample should show you how to create the overlay, and then instead of creating an overlay list containing the cities, it instead should just have a single item which is your current location.
It's a little unclear what you are asking but you are probably looking for the MyLocationNewOverlay. This will monitor the GPS and plot an icon at your location. The OpenStreetMapViewer sample covers this. The icon will move as your location moves so if you are looking to put a permanent marker that stays put then you want to use an ItemizedIconOverlay.
This seems to be the minimum amount of code to get a point to display. You need a "blue_pin" image in drawable and a layout with "mapview" component.
public static final GeoPoint berlinGeoPoint = new GeoPoint(52.516667, 13.383333);
OverlayItem overlayItem = new OverlayItem("Berlin", "City", berlinGeoPoint);
Drawable markerDrawable= this.getResources().getDrawable(R.drawable.blue_pin);
overlayItem.setMarker(markerDrawable);
ArrayList<OverlayItem> overlayItemArrayList = new ArrayList<OverlayItem>();
overlayItemArrayList.add(overlayItem);
ItemizedOverlay<OverlayItem> locationOverlay = new ItemizedIconOverlay<OverlayItem>(this, overlayItemArrayList, null);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.getOverlays().add(this.locationOverlay);
Related
I have placed multiple Google Markers in my application in different locations and streets:
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(26.89209, 75.82759))
.title("Food"));
What I want is to show users only the marker near their current location. For example if the user is in XYZ street then only the XYZ street marker will display
Check this Viewport Marker Management documentation that works by getting the current bounds of the map that is visible to the user, then — based on the map's bounds — a query is sent to the server to get all the markers that lie within the bounds.
Only the markers in the square are loaded, this reduces load time and
improves performance and as the box moves only the needed markers are
shown and the rest are removed.
First, attach a listener to the map idle event to make it execute a function called showMarkers() when the map stops panning.
google.maps.event.addListener(map, 'idle', showMarkers);
Then add the function showMarkers that gets the current viewport bounds by calling map.getBounds() which returns a object that has the NorthEast and SouthWest LatLng points of the bounds. With the bounds information you can send a request to your server (preferably with AJAX) to get the markers that are within those bounds.
function showMarkers() {
var bounds = map.getBounds();
// Call you server with ajax passing it the bounds
// In the ajax callback delete the current markers and add new markers
}
With the new list of markers you can remove the current markers, marker.setMap(null), that are on the map and add the new ones, marker.setMap(map).
Hope this helps!
I'm trying to implement app in which you can add your own marker which will be given a shutdown time. To do so I need to know how to manage my markers. Let's say I have array list of users with assigned unique ID.
private ArrayList<User> userList = new ArrayList<>();
Then I will create array list of markers which will contain information like Latitude, Longitude, Title, owner's ID and deadline.
private ArrayList<MyMarker> mMarkersArray = new ArrayList<MyMarker>();
Next whenever user will activate add marker method, new marker will be pushed to my list of markers. Ideologically everything seems nice and easy, furthermore creating new object looks like this:
Marker mMarker = mMap.addMarker(new MarkerOptions() (...) );
but when it comes to managing specific markers it seems like I'm missing something. There will be some trigger method which will check the deadline of all markers (or rather first one on the 'sorted by deadline' list) and then it should remove (not hide, because I think it would be inefficient from the memory point of view). How to achieve this? I can't add some custom variable like ID to markers (so I could then find the one I'm interested in) and I'm a bit lost.
There is a way to achieve this by clearing whole map and then rendering again all markers except the inactive, but as far as I'm concerned it's very inefficient and there has to be better solution.
If you want to remove a specific marker from the map, you can just call the remove(). method.
Sample code to remove a marker from MapView and HashMap:
for (Marker marker : hm.keySet()) {
if (hm.get(marker).equals(deadline)) {
marker.remove();
hm.remove(marker);
}
}
You dont need to clear the entire MapView, if you just call remove() method on specific marker.
I've some ItemizedOverlay adding onto MapView. I'm able to show case the static marker which is been initialized initially. Now say after 5 markers, I need to update the marker of the first or second one precisely, how can I do it? I want to refresh or update the previous markers which is already visible, how to achieve this?
public class MyItemOverlay extends ItemizedIconOverlay<OverlayItem> {
public MyItemOverlay(ArrayList<OverlayItem> pList,Drawable marker, ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener, ResourceProxy pResourceProxy) {
super(pList, marker, pOnItemGestureListener, pResourceProxy);
}}
You should hold a list of either Markers related to the MapView in the current context or hold references to the OverlayItem (which may contain marker(s)) inside. This will allow you to access the markers by reference at any point.
my problem is as follows.
I am creating multiple itemized overlays. (because every overlay gets a different drawable)
I customized the itemized overlay class, but when i add it to the mapview overlays, the class is transformed into an overlay class.
to make it worse i got 3 classes creating overlays on the same map. each class represents an item on the map with it's own intelligence behind it.
the problem i now have is that i want to remove an overlay, but i can not be sure that the index i inserted it on, is also the index it has when i try to remove it. (the other classes might have inserted an overlay in the mean time)
the classes are self updating, so i do not want a solution that fires an update or delete event from the main class. (the whole point is to add a class and forget about it)
so my question would be: how can i identify which layer is which when i want to call a remove on that layer. i think the information is available, but i do not know how to get to it.
this is the code i am using to add the overlay
OverlayItem overlayitem = new OverlayItem(p,myNaam ,myOmschrijving );
LocationOverlay = new MyLocationOverlay(drawable, myContext);
LocationOverlay.SetLocation(i,overlayitem);
myOverlays.add(LocationOverlay);
You can set a certain integer as a position for every overlays
something like that :
mapView.getOverlays().add(0,myScaleBarOverlay);
and when you want ro remove this call:
mapView.getOverlays().remove(0);
mapView.invalidate();
Regard
You don't have to remove specific layer. You can remove an overlay specified by it's reference (e.g. myOverlay).
LocationOverlay myOverlay = new MyLocationOverlay(drawable, myContext); //`you forgot the name of variable`
mapView.getOverlays().remove(myOverlay);
I tried searching the forums on this one, but I wasn't able to find anything on my problem.
To describe my problem, everytime my location changes, it redraws the center maker on the map.... Only catch is that it doesn't delete the previous one. I can get it to delete the previous one when the location is changed, but I have no idea how to pass the original overlay in-between classes.
Also, pastebin here
Thanks in advance,
hwrd
You need to clear "existing" items in the Overlay list before adding new ones.
public void createOverlay(GeoPoint point, MapView mv)
{
//Make overlay reference declaration
List mapOverlays = mv.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.center_marker);
FindScreenOverlays itemizedoverlay = new FindScreenOverlays(drawable);
OverlayItem overlayitem = new OverlayItem(point,null,null);
//clear your list before adding new overlays unless you want to see all the previous locations as well.
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
Adding an OverlayItem is similar to adding an overlay. Just extend ItemizedOverlay. ( public class YourItemizedOverlay extends ItemizedOverlay )