Is it possible to batch adding polygons to GoogleMap View (APIv2) - android

I'm drawing a bunch (50-500) polygons on a Google MapView (API v2) in Android 4.0+, and trying to modify them with a touch event. The touch event I trap via dispatchTouchEvent(), and it's working, just very slowly. The performance is bad enough that it doesn't 'feel' like it's working.
I believe the problem lies in the fact that I have to loop through a collection of polygons, adding them to the map one at a time, so the map redraws with each one.
Looking through the documentation I cant see a way to add multiple polygons at once, but maybe I'm missing something. Any help would be appreciated.
My dispatchTouchEvent looks something like this:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(mInDrag||mInRotate||mInAB){
if (ev.getAction()==MotionEvent.ACTION_DOWN){
mStartPt = mMap.getProjection().fromScreenLocation(new Point((int)ev.getX(), (int)ev.getY()));
mLastDragTime=System.currentTimeMillis();
} else if (ev.getAction()==MotionEvent.ACTION_MOVE){
LatLng endPt = mMap.getProjection().fromScreenLocation(new Point((int)ev.getX(), (int)ev.getY()));
long iterationTime = System.currentTimeMillis()-mLastDragTime;
//checking iterationtime so grid doesn't move after the user stops dragging
if ((!mStartPt.equals(endPt))&&iterationTime<300) {
LatLng delta = new LatLng(mStartPt.latitude-endPt.latitude, mStartPt.longitude-endPt.longitude);
mLastDragTime=System.currentTimeMillis();
if (mInDrag) {
//setup calcs
} else if (mInAB){
//setup calcs
} else if (mInRotate){
//setup calcs
}
clearTempGrid(); //clears the grid including erasing the existing polygons on the map.
int gridType = gridTypeSpin.getSelectedItemPosition();
for (PolygonOptions newPolygon : myPolygonGrid.modify(A, B)) {
//I run into problems here. Even 150 polygons slows it down
Polygon myPolygon = mMap.addPolygon(newPolygon);
tempPolygons.add(myPolygon);
}
}
}
}
return super.dispatchTouchEvent(ev);
}

Are you sure what you want is to modify all Polygons when dragging?
Add Polygons once and not clear and recreate them every time you get touch event.
Modify them (all?) with Polygon.setPoints(...).

Related

How to adjust zoom level to fit boundary and then center map in marker offset?

I have a google map (com.google.android.gms.maps.GoogleMap) where I have some markers set.
I am able to, separately,
1) adjust zoom level and center the map on a boundary:
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10));
and
2) center the map above one of the markers:
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude
+ offset, markerSelected.getPosition().longitude);
mMap.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
but, for the life of me, I can't just do both, adjust the zoom level using newLatLngBounds and then center the map somewhere else. Whatever I do last is what I see happening in the map.
How do I do this?
For future visitors this is how you can chain camera animations:
map.animateCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10), 2000, new CancelableCallback() {
#Override
public void onFinish() {
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude + offset, markerSelected.getPosition().longitude);
map.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
}
#Override
public void onCancel() {
}
});
Also see AnimateCameraChainingExampleActivity.java for an example how to chain infinitely.
Try using both moveCamera and animateCamera...
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10));
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude
+ offset, markerSelected.getPosition().longitude);
mMap.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
moveCamera will move directly to that spot while animateCamera will provide the moving effect. They are linear in nature so one will happen after the other however layering them as I have done above will provide the potential effect you are looking for.
If you are trying to see the actual movement of both calls on the UI you will need to register for the callback post the completion of the animation as needed.

Enhance Performance of mapview with 200+ markers

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();
}

MapView very slow with 1000 overlayitems on the map, even with only 2 visibles

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
}

Previous coordinates of a touched object

I'm programming with Processing, what i need is the equivalent of pmouseX/Y but for touch, but I can't use pmouse because I use multi-touch and I need previous coordinates of each touched point. I don't know if I've made myself clear, to do an example I need to know initial and final coordinates of a swipe
I currently use to get initial coordinates:
public boolean surfaceTouchEvent(MotionEvent me) {
float x0=me.getX(0);
float y0=me.getY(0);
....
....
return super.surfaceTouchEvent(me);
}
I'm not sure if I get your right here, since this seems to be very basic programming, but I'll try.
Just use an ArrayList and add each position there. For different touches, you might want to use a HashMap, like this:
HashMap<MotionEvent, ArrayList<Point2D.Float>> touches = new HashMap<MotionEvent, ArrayList<Point2D.Float>>();
public boolean surfaceTouchEvent(MotionEvent me)
{
float x0=me.getX(0);
float y0=me.getY(0);
if (!touches.containsKey(me))
{
touches.add(me, new ArrayList<Point2D.Float>());
}
else
{
// get previous position
Point2D.Float prevpos = touches.get(me).get(touches.get(me).size() - 1);
}
touches.get(me).add(new Point2D.Float(x0, y0));
....
}
Didn't test this, but that's basically how it would work.

Autoadvancing gallery animation

I have a Gallery widget set up to auto advance every 10000ms. However the transition between views is instantaneous and I would prefer to have a transition.
My advancing method is like so:
private void tickerAdvance() {
int selectedItemPosition = mTickerGallery.getSelectedItemPosition();
if (selectedItemPosition + 1 == mTickerGallery.getCount()) {
mTickerGallery.setSelection(0, true);
} else {
mTickerGallery.setSelection(selectedItemPosition + 1, true);
}
}
I was under the impression that setting the animation to true would cause it to animate between states. In my XML I've also added animationDuration="500", however the transition still pops between states instantly.
The problem is because you have used the setSelection() which obviously won't give you the feel of an Gallery being scrolled. SO instead of using setSelection() you have to override the onScroll() of gallery.
Here is how you do it.
Assuming that you have made the necessary steps for auto advancing periodically, now do this code.
MotionEvent e1, e2;
gallery.onScroll(e1, e2, scroll_limit , 0); //scroll limit is your integer variable for scroll limit.
The answer I came up with is that the Gallery API is terribly non-extensible. There are multiple package private or private methods that would unlock this functionality without a kludge including moveNext, FlingRunnable.startwithdistance, and scrollToChild. Alas, they are all unavailable.
Instead the best answer I was able to come up with was to override setSelection to get all the behaviors I needed. In the setSelection method I reverse the deceleration algorithm to calculate a velocity for a calculated distance and then call onFling. This is a nasty kludge could be made a ton better by making any of the above methods protected or public (I can't fathom a reason why at least moveNext and scrollToChild shouldn't be).
i had a similar task to make item to item animation and i decided to choose gallery, so i tried a lot of things, but the best way is to subclass gallery and add this:
boolean avoidSound = false;
public void showNext() {
avoidSound = true;
this.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
public void playSoundEffect(int soundConstant) {
if (avoidSound) {
avoidSound = false;
} else {
super.playSoundEffect(soundConstant);
}
}
just call showNext() and it will do an animation

Categories

Resources