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.
Related
I am using Google Maps in Android with about 800 markers that I want to cluster and 80 markers that I do not want to cluster.
For the markers that I want to cluster, I add to the clusterManager using
mClusterManager.addItem(annotation);
For the markers that I do not want to cluster, I add them directly to the markerCollection
Marker marker = mClusterManager.getMarkerCollection().addMarker(annotation.getMarkerOptions());
Here annoation refers to an instance of AirMapMarker which is a custom class that implements ClusterItem
annotation contains a custom information such as identification string that I need to use when user taps on the marker.
The problem is, I cannot register onClickListener for both the cluster and the mapMarker. If I do, only map's onMarkerClickListener fires.
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
// Handle marker click fires correctly.
}
}
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<AirMapMarker>(){
public boolean onClusterItemClick(AirMapMarker marker) {
// Doesn't Fire
}
}
Now the problem is, inside onMarkerClick, I need access to identification which is part of AirMapMarker, in the case if the marker click happened on a single unclustered marker. I could not figure out a way to map Marker to AirMapMarker inside onMarkerClick.
I also tried completely removing map.setOnMarkerClickListener and rely only on mClusterManager.setOnClusterItemClickListener. However, now the problem is that when the marker click happens on markers that are not part of the cluster, onClusterItemClick fires with parameter null. This is because when adding marker to the map, you provide markerOptions and not ClusterItem object.
There is option inside ClusterManager to turn off clustering but this happens globally and not for a particular markers.
Anyone help me out or give me suggestions on what I should try next?
I don't know if I understood your question right. But here is a possible solution.
The markers you want to cluster, you can add them to the ClusterManager.
clusterManager.addItem(yourItem);
The ones you don't want to cluster, you can add them directly to your GoogleMap object.
map.addMarker
Inside your MarkerRenderer you can override
protected void onClusterItemRendered(YourClusterItem clusterItem, Marker marker)
and map yourClusterItem to your Marker if needed. Or you can set a tag to your marker indicating it's a clusterItem.
In public boolean onMarkerClick(Marker marker)
you can check your tag (or your map) to have a different behaviour for each kind of marker.
I have a problem with using Google maps API on Android. I have a button which removes the selected marker off the maps interface, and information about it from the SQLite db that I have set up. Although my only problem is once multiple markers are on the map this feature stops and doesn't remove them:
Below shows the method which removes the marker from the maps and replaces them. As I said this works perfectly fine with a single marker and my testing has been a success, but with multiple it does not work.
I have an onclicklisterner for the markers which displays the information and a popup box for the marker, for the user to remove the marker they must click the marker which sets the global variable to that object then once the remove button is pressed the removeMarker() method is called. The getAllMarkers() method loops through a SQLite db and pulls information and adds to the maps.
End problem: Removing a marker when multiple markers are placed on the map doesn't work. Only works when a single marker is placed on the map.
Marker lastOpened = null;
To remove the information from the db the condition in the if statement returns a boolean value if it has been done:
if(this.mDbHelper.deleteLine(lastOpened.getTitle()))
Remove method
public void removeMarker(){
if(this.lastOpened != null){
if(this.mDbHelper.deleteLine(lastOpened.getTitle())){
lastOpened.remove();
getAllMarkers();
}
}
}
Thanks
You can either use googleMap.clear() or you can store your Markers in a collection of kind and remove them in a loop:
private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
Here's a related ticket discuss how to remove marker: Remove a marker from a GoogleMap
I have a map in which I am creating different types of markers. I cannot assign an info window adapter to a marker (gee wouldn't that be nice), I can only assign on InfoWindowAdapter for the entire map (at least I think).
My problem is that I want to show a different type of info widow depending on what I clicked. Id the only way to set one InfoWindowAdapter that will handle creating the correct type of info window based on the marker that I am passed?
Am I missing something easy?
When you add a marker to the map, you are receiving back an ID, which uniquely identifies your marker.
You can create an instance of your InfoWindowAdapter immediately after you add the marker and put it in a map, which keeps the ID as key and your InfoWindowAdapter as value.
Marker marker = map.addMarker(options);
// Create your special infoWindowAdapter for this marker
// ...
adapterMap.put(marker.getId(), youSpecialInfoWindowAdapter);
In the one central InfoWindowAdapter, which you register at the map, you can just use the ID of the marker to get the specific InfoWindowAdapter and delegate to the methods of that.
Access to the map can e.g. be provided in the constructor of the InfoWindowAdapter (to avoid global or static variables):
class CentralInfoWindowAdapter implements InfoWindowAdapter {
Map<String, GoogleMap.InfoWindowAdapter> adapterMap;
public CentralInfoWindowAdapter(
Map<String, GoogleMap.InfoWindowAdapter> adapterMap) {
this.adapterMap = adapterMap;
}
#Override
public View getInfoContents(Marker marker) {
InfoWindowAdapter adapter = adapterMap.get(marker.getId());
return adapter.getInfoContents(marker);
}
#Override
public View getInfoWindow(Marker marker) {
InfoWindowAdapter adapter = adapterMap.get(marker.getId());
return adapter.getInfoWindow(marker);
}
}
You can vary this principle of course. If you have only a few different InfoWindowAdapters depending on the "type" of the marker, you may put an enumeration into the map, which identifies the type of your marker and lets you decide, which kind of real InfoWindowAdapter to use inside the central InfoWindowAdapter, or you may still put instances of your special InfoWindowAdapter into the map, but use the same instance for the same type of marker.
if i am right you want to show a different window adapter on each marker?.. if yes you can add a tag on each marker then inside one of the two infowindow function either infowindow() or infocontents() checks the marker tag and add the appropriate layout.
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);
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 )