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();
}
Related
I am having 3 types of overlays on the map.
1) there is one kind of overlays which is having simple markers. And on click of overlay i show pop up.
2) other kind of overlay contains canvas drawing. and onTouch event of these overlays we have to show third kind of overlay which contains bitmap drawing on the map. And click event of third kind of overlay is also there.
so, due to heavy calculations and lots of canvas drawing, performance of mapview is degraded.
I have gone through Overlaymanager libreary from https://github.com/staroud-android/OverlayManager. but its not helping me.
Is there any way which can fulfill my requirements with high performance?
I also need suggestion that Polaris map library can help me for better performance? https://github.com/cyrilmottier/Polaris
What is happening is that you are populating the MapView everytime you add a GeoPoint. Try adding this to your code:
after you have looped through the GeoPoints place this code
itemizedOverlay.populateNow();
and change you itemizedOverlay to look like this:
public void addOverlay(OverlayItem overlay) {
m_overlays.add(overlay);
}
public void populateNow()
{
populate();
}
#Override
protected OverlayItem createItem(int i) {
return m_overlays.get(i);
}
#Override
public int size() {
return m_overlays.size();
}
I am developing one Map project in Android. In this App, Some cluster icons are placed on the map. I want to set a listener for cluster icon click. So i decide to override a method onTap() in my custom overlay class (say ClusterMarker). How could i set listener class for this?
Please provide me the right way to do this.......
like,
ClusterMarker (my custom Overlay class)
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
Projection pro = mapView.getProjection();
Point ct = pro.toPixels(center_, null);
Point pt = pro.toPixels(p, null);
//....
....
...
return false;
}
Thanks,
I found how to create listener class for onTap Event. But actually listener class not needed here. Because My custom Overlay in turn do that work. For that I have to add my custom overlay object to mapview.
I have a mapview showing 1000 overlayitems on the mapview. The map moves very slow even if only two overlayitems are visibles on the map (huge zoom applyed)
It is possible to optimice the mapview to move more fast if only low cuantity of overlayitems are visibles?
Thanks
You might have fallen in the usual pit. If you've made your own customoverlay make sure that your addOverlay methode don't populate. Populate should first happen when all overlays has been added. If you populate each time you add you'll end up with many overlays on top of each other.
Make sure it uses 2 methods for this process, 1 to add and 1 to populate:
public void addOverlay(CustomOverlayItem overlay) {
mOverlays.add(overlay);
}
public void populateOverlay() {
populate();
}
If that is not the issue then you should look into dynamically removing pins when they are outside the screen range or grouping pins together. I believe there is some open source project that have done the work for you in this regard.
Found a link http://onthefencedevelopment.com/blog/using-google-maps-your-android-applications-%E2%80%93-part-3-adding-place-markers drawing only the overlays which are on the screen. haven't tested it yet but seems promising.
EDIT: it works
subclass MapView, override dispatchDraw(). Detect changes in map center or zoom level:
#Override
public void dispatchDraw(Canvas canvas)
{
super.dispatchDraw(canvas);
GeoPoint gpCenterPoint = getMapCenter();
int nZoomLevel = getZoomLevel();
// either diff't zoom level or change to center point?
if ( (gpCenterPoint.getLatitudeE6() != mCenterPoint.getLatitudeE6()) ||
(gpCenterPoint.getLongitudeE6() != mCenterPoint.getLongitudeE6()) ||
(nZoomLevel != mZoomLevel) )
{
// update pins here
updatePINS();
mZoomLevel = nZoomLevel;
mCenterPoint = gpCenterPoint;
}
}
void updatePINS()
{
int nLatitudeSpan = mapView.getLatitudeSpan();
int nLongitudeSpan = mapView.getLatitudeSpan();
// TODO insert/remove PINS as needed
}
I'm using an ItemizedOverlay containing a list of OverlayItems to display markers on top of a MapView. My problem is that when an OverlayItem uses the default marker defined for the overlay, its getMarker() method returns null. Is there another way I can get that marker image? I'd like to see if I can animate it during onTap().
I'm showing a bunch of markers on the map, many of which use the same image, so I thought it best to make use of the overlay's default image to save memory. For this app it's possible there could be more than 100 markers displayed for a given overlay.
You have a couple of different ways how to address this. I'm showing the simplest one I know, if it don't solves your problem, let me know.
public class MyItemizedNewOverlay extends ItemizedOverlay<OverlayNewItem> {
private ArrayList<OverlayNewItem> myOverlays ;
Drawable defaultMarker;
Context context;
public MyItemizedNewOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
this.defaultMarker = defaultMarker;
this.context = context;
myOverlays = new ArrayList<OverlayNewItem>();
populate();
}
. . .
}
With this you can use the default marker in your code for the onTap()
I'm working on a map in Android, with several overlay icons covering it. My problem is that each overlay's icon is static and does not resize with the rest of the map when zooming in and out. This can become awkward if the user zooms too far in or out and the icons appear too big or too small.
My solution is to create multiple image files with different sizes of each icon. However, I'm not sure how to reference these icons from my (extended) MapView class after I've already created them from my :
public class Map extends MapActivity {
...
public onCreate() {
...
//Very simplified copy of my working code:
Drawable defaultDrawable = this.getResources().getDrawable(R.drawable.myicon);
List<Overlay> mapOverlays = mapView.getOverlays();
ItemizedOverlay overlayManager = new OverlayManager(defaultDrawable, this);
OverlayItem myOverlay = new OverlayItem(...);
overlayManager.addOverlay(myOverlay);
}
}
I extended the MapView class so that I could trigger zoom events. The dispatchDraw() method is where I plan to place my icon-replacement code, but I'm not sure how I can access the map's existing overlays and their icons from here (and change just their icons):
public class ZoomSensitiveMapView extends MapView {
private int oldZoomLevel;
...
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (getZoomLevel() != oldZoomLevel) {
if (getZoomLevel() > 15) {
//Zoomed in closer
//Replace icons with bigger copies
}
else {
//Zoomed out further
//Replace icons with smaller copies
}
oldZoomLevel = getZoomLevel();
}
}
}
I was thinking getOverlays() might work here, but that seems to only return the overlays themselves, not their icons.
Am I on the right track? How can I replace the icons without adjusting their overlay coordinates, etc.? Any help or recommendations would be greatly appreciated here! (Hopefully my explanation is clear enough; if anything is not, please ask me to clarify!) Thanks in advance for your help!
here you can change with line ::
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
just put your own icon in drawable folder and update with
Drawable drawable = this.getResources().getDrawable(R.drawable.yourIcon);
For more information :: get from here