android map items not cleared - android

I'm repeating the following code every x seconds in my MapActivity:
mapview.invalidate();
for (int i=0; i<mapview.getOverlays().size(); i++ )
{
mapview.getOverlays().remove(i);
}
mapview.getOverlays().clear();
List<Overlay> mapOverlays = mapview.getOverlays();
//set new geoponts here and add 2 markers via itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapview.invalidate();
Though I can see on the GUI that it's not refreshing the page, it's always drawing on top of it. It doesn't seem to clear the content before putting new stuff on.
Also using postinvalidate() doesn't work
Any idea what I'm doing wrong?

The reference of itemizedoverlay was not in that function, I moved it there and now it works.

Related

Android: MapView does not react on touch events under some layers of WiFiSLAM

I am using the indoor positioning framework wifislam and have a programming question.
So far I have given a MapView by the indoorLocationView of WifiSLAM. I'm working with the following code.
final IndoorLocationManager.Configuration config = new IndoorLocationManager.Configuration();
indoorLocationManager = new IndoorLocationManager(this, config,
onServiceConnected, LOCATION_CHOICE);
indoorLocationView = indoorLocationManager.createLocationView(this,
GOOGLE_MAPS_API_KEY);
final View mapContainer = findViewById(R.id.mapcontainer);
((ViewGroup) mapContainer).addView(indoorLocationView.getView());
MapView mapView = indoorLocationView.getGoogleMapView();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pinred);
itemizedOverlay = new AddItemizedOverlay(drawable, mapView.getContext(), sm);
mapOverlays.add(itemizedOverlay);
and the AddItemizedOverlay has following methods:
#Override
public boolean onTap(GeoPoint geopoint, MapView mapView) {
mapOverlays.clear();
// geopoint = mapView.getProjection().fromPixels((int) event.getX(),
// (int) event.getY());
// latitude
if (geopoint != null) {
this.geopoint = geopoint;
lat = geopoint.getLatitudeE6();
// longitude
lon = geopoint.getLongitudeE6();
OverlayItem overlayitem = new OverlayItem(geopoint,
"Task Position", "Derzeitige Position vom Task");
this.addOverlay(overlayitem);
}
return false;
}
My problem is now, that the onTap method does not respond or react on touching on the device. As well the onTouch event has no reaction. I think those layers above of wifislam are preventing that I can reach the MapView. Is there any possibility to reach the MapView so I can add some markers on the map?
The reason for this is that there is a transparent view on top of the Google Maps view that is showing the floorplan. Try adding a listener on the view returned by IndoorLocationView#getView, but that may screw up the pinch-zoom/panning controls; you should double-check the effect (you probably want to return false from your listener unless one of your markers has been tapped).
If that doesn't work, your best bet is to contact WiFiSLAM support with your use case so they can update their SDK to meet your needs. Looks like there's a support email address in the FAQ topic of the help section of the SDK website.

How to create a static Mapview that recognize an onTap method

I'm trying to develop a layout that shows a Google Map with a route drawn on a Overlay layer associated with this map. I want this mapView to be a small static map that shows, as a thumbnail, the main route the user followed, and then if the user clicks on it, an intent takes you to a different activity displaying the map in full screen with the route and all the zoom functionalities.
The thing is that although I override the onTap method of the Overlay setting there the intent to the new activity, it only works if I set the MapView as setEnabled(true), but if I do so, then the thumbnail map can be dragged and moved by the user.
I'm sorry if it is not clear enough, but I don't know how to explain it better.
Thanks in advance
This is my customized class which extends Overlay and overrides the onTap method:
class MapOverlay extends Overlay {
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
Intent i = new Intent(getApplicationContext(),
RouteMapActivity.class);
startActivity(i);
return false;
};
And this is my onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detailrun_activity);
map = (MapView) findViewById(R.id.mvMain);
map.setEnabled(true);
map.setClickable(true);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = map.getOverlays();
projection = map.getProjection();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
double lat = 28.063119 * 1E6, longi = -82.41128 * 1E6;
GeoPoint point = new GeoPoint((int) lat, (int) longi);
MapController myMapController = map.getController();
myMapController.setCenter(point);
}
For now I'm only drawing a straight line between two GeoPoints, and I don't get any logcat errors.
The map with the route is indeed drawn, and the overrided onTap method works, but it seems it only recognizes the tap if my mapView is defined as enabled and clickable, but if I do so, then the user can also move and drag the map as long as he holds pressing the screen.
you can get static google map image as per your location by this link. that you can display in imageview and click on that you can target new activity which have MapView.. i think it may be a good option for you.

Bug in google maps?

I was following this tutorial to create something a little diffrent, this tutorial showed how the overlay works with predefined items inserted to it, but what if i want to dynamiclly add items to it? then I must start with an empty overlay:
Instead of :
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
ItemOverlay itemizedoverlay = new ItemOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
which works just like as expected in the tutorial, I need to put this:
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
ItemOverlay itemizedoverlay = new ItemOverlay(drawable, this);
mapOverlays.add(itemizedoverlay);
which ommits the initializition of the overlay with an item.
this approach leads to the map getting stuck, and after few taps my app gets kicked out due to null exception.
1.is this expected behavior? it seems like a bug to me...
2.what do i do in order to acheive the expected behavior?(in which i can start with an empty overlay and add items to it dynamically)
And as for what i want to acheive, which is to not initiate the overlay with and item but to use addOverlay dynamically, this way i can
As far as I understand, under ItemOverlay you mean ItemizedOverlay. If so, you are on the right track. ItemizedOverlay is a container of map items, so every time you add/delete items from ItemizedOverlay you need to set last focused item to -1 and call populate() method to notify mapview about content change.
Just remember that you need to make changes in ItemizedOverlay content in a UI thread context ;)
There is also undocumented trick here. You need to set last focused index to -1 and do populate() even when you don't have items (so you need to do this on initialization stage)
So your code should look like:
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
ItemOverlay itemizedoverlay = new ItemOverlay(drawable, this);
mapOverlays.add(itemizedoverlay);
setLastFocusedIndex(-1); //reset initial position
populate(); //notify about our content
//now try to add new item
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
setLastFocusedIndex(-1); //reset initial position
populate(); //notify about our content

Remove overlay in the map

My application search and visual same overlays. But, in the next search the old overlay are not clear. I have to try to use map.getOverlays().clear(); but it remove also the overlay of MyLocationOverlay. There is a method for delete only same overlay?
I know its a little too late, but since i came up with a solution that helped me, i thought of sharing it so that it helps programmers in the future..
#hooked82 : the problem with doing just
overlays.remove(position)
is that its a list. so when you remove one item from the list, every item to the right of this item, in the list, is shifted one place to the left, which will eventually mess up your positions making it really tedious to keep a track of them all over time.
During one of my projects, i faced the same issue. Since i was adding and removing overlays upon some updates, after a point of time, it was really difficult to keep a track of the overlay positions in the list.
Here is what i did as a work around for the problem, and so far there have been no issues.
Step 1: create your custom Overlay Class
public class MyOverlay extends Overlay
{
private String overlayUUID;
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
-- do what you need to do here --
}
public String getOverlayUUID()
{
return overlayUUID
}
}
Step 2: in the MapClass where you handle the overlays. Please be sure that the mapOverlays list is a global list in the MapView class where you add all your mapOverlays
List<Overlay> mapOverlays = mapView.getOverlays();
public void removeChosenOverlay(String overlayUUID)
{
for(int i=0; i<mapOverlays.size(); i++)
{
String className = mapOverlays.get(i).getClass().getSimpleName();
if(className.equals("MyOverlay"))
{
MyOverlay myOverlay = (MyOverlay) mapOverlays.get(i);
if((myOverlay != null) && (myOverlay.getOverlayUUID.equals(overlayUUID)))
{
mapOverlays.remove(i);
mapView.invalidate()
break;
}
}
}
}
With the above method, one can uniquely identify each overlay with a UUID (in my case it was a string, but you can custom create your UUID's in any format you wish to) and remove the corresponding overlay item successfully from the list of overlays, without having to worry about their relative positions in the list.
I hope it helps others who face the same issues with uniquely identifying overlays.
When you add overlays to your map, you can add it to a certain position
overlays.add(position, overlay_object);
Then when you need to remove specific overlays, you can remove them in the same fashion
overlays.remove(position);
you can do this with the help of the counter and another reference to the overlayItem to save old overlay which you want to remove
in my case counter is current_overlay_counter and old_overlay_item get the reference for old overlay
on the first call the counter is 0 so there is no overlay to remove
public void getcurrentPostion()
{
GeoPoint gpoint=new GeoPoint((int)((current_location.getLatitude())*1E6),( (int)((current_location.getLongitude())*1E6)));
OverlayItem overlayitem = new OverlayItem(gpoint, "Hola, Mundo!", "I'm in Mexico City!");
mapOverlays = map.getOverlays();
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
if(current_overlay_counter>0)
{
System.out.println("deleting overlay");
itemizedoverlay.removeOverlay(old_overlay_item);
map.invalidate();
}
old_overlay_item=overlayitem;
current_overlay_counter++;
map_controller.setCenter(gpoint);
}
you need one more thing ,a method to remove the overlay in your ItemizedOverlay class implementation
this method takes the referece of the overlay you want to delete
public void removeOverlay(OverlayItem overlay){
mOverlays.remove(overlay);
System.out.println("removeOverlay is called");
populate();
}

android mapview overlay popup hide

i have a mapview and overlay items which when tap on shows popuo with text. what I want is when user taps on map outside of these overlay items or popup hide the popup if its currently visible.
The only way to do it (as I know), is adding new overlay to your map, witch serves as the popup. When you tap outside of the overlay, just remove it from overlay list.
This means you have to draw and handle events for your popup by yourself.
Ive been trying to acheive the same, this is exactly what i want :-
http://proxy.latest.xuemath.appspot.com/img?s=aR.1f.1be6ifej.2ec/_Fbk8IlxNQXM/S-ruq97dRWI/AAAAAAAAELY/s-o3onSReiU/s400/kml_google_maps_v3.bmp
i have implemented googleMapView with overlays, i have one issue i want to show a popup when clicked on each overlay, and when i click on another overlay the previous popus should disappear and new one should appear at the clicked location(ie projection points). And popup shouldnt appear when i click anywhere else on screen.Im using onTap event to record onclick. (map_overlay) is the layout that i want to show when someone click a projection point on the map. The code is below and map_overlay.xml could be any file.
Class: ItemizedOverlay:-
public boolean onTap(GeoPoint p, MapView mapView) {
LayoutInflater inflater = (LayoutInflater)cContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams lp = new MapView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, p, LayoutParams.WRAP_CONTENT);
LinearLayout view = (LinearLayout)inflater.inflate(R.layout.map_overlay, null);
mapView.removeView(view);
mapView.invalidate();
mapView.addView(view,lp);
mapView.invalidate();
return true;
}
Below is the Main class in which im displaying projection points which is working fine and im calling I have implemented the onTap event in another class as shown above:
public class MapView extends MapActivity{
private ArrayList overlayItem ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.large_mapview);
mapView = (MapView) findViewById(R.id.mapview);
// mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mappointer2);
ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable,this);
OverlayItem overlayitem;
GeoPoint point;
double lat;
double lng;
for (int i = 0; i < overlayItem.size(); i++) {
lat = Double.parseDouble(overlayItem.get(i).getLatitude());
lng = Double.parseDouble(overlayItem.get(i).getLongitude());
point = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));
overlayitem = new OverlayItem(point, i+"".toString(), overlayItem.get(i).getDetails().toString());
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
mapView.invalidate();
}
}

Categories

Resources