How to catch a map panning | zooming finished event with Android SDK? - android

I am looking for a way to receive events when a map finished panning or zooming or panning and zooming. Those events are not fired, afaik, from MapActivity in the SDK for Android 2.3.3 which I use. I tried the MapChange library, however, it forces you to use Handler in your MapActivity to ensure to that UI updates are processed on the UI thread; more information.
Question: Do you know of any support for such events by the Android Support Library e.g. by using FragmentActivity instead of MapActivity - I could not find an alternative solution by now.
Similar questions can be found here:
Android MapActivity on map movement
How to catch that map panning and zoom are really finished?
Edit:
The MapChange library was updated some hours ago. I will check out what has been improved.
Edit:
The MapChange library has been improved and works much more reliable now and is a good option! Please checkout the GitHub project for further information. However, I leave the question open since I am still interested whether one can do the same with existing components.

You can use the GoogleMap.OnCameraChangeListener to assign a listener to the changing camera event. Handling that and other events is discussed in the Google Maps Android API documentation.

Why is it a problem to use a Handler ? You can always post your UI changes to the main thread, why not do that ?
Someone on SO found a way to get a Fragment activity to contain a MapView by changing the support library code but it became too cumbersome for me to use and I reverted to a full-blown Map activity.

For anyone still looking for an answer to this
googleMap.setOnCameraIdleListener {
//Do your thing
}
called when camera movement has ended

Related

GoogleMap.OnCameraIdleListener Mapbox equivalent

I am looking for an GoogleMap.OnCameraIdleListener equivalent in the Mapbox Android SDK.
Usecase
When panning around the map I need to query some geojson from a server, which is to be shown on the map.
For this I need to be able to detect when the map have moved and is once again idle. Google Maps have the OnCameraIdleListener and OpenLayers have the 'moveend' event. I can't use Google Maps due to some licensing issues and I'm trying to find a native alternative to OpenLayers, which I currently use.
What I have tried
I have tried to use various listeners offered by the MapboxMap class, but all of them is called much too often. Like the following:
map.setOnCameraChangeListener(new MapboxMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
Log.d("TAG", map.getProjection().getVisibleRegion().latLngBounds.toString());
}
});
The method is called a lot of times (every 400ms I think during panning). If I were to use the method directly the server load would be much too large as well as handling the returned data.
I could make a workaround that only calls the server if no CameraChange event has occurred for 5-800ms, but it would result in some not-to-nice code.
Possible duplicates
I found the following in the MapBox github:
https://github.com/mapbox/mapbox-gl-native/issues/4746
The issue seem very similar. I have tried the proposed solutions, but without a satisfying result.
Have I missed some implementation or does someone know of a nice workaround?
Thanks in advance.
Functionality added in v5.1.0
https://github.com/mapbox/mapbox-gl-native/pull/8644

How to prevent android maps from scrolling outside boundaries

I've been unable to find an example how to limit scrolling of android Google maps to defined coordinates or preferably not outside a ground overlay.
There are some examples of extending MapView and overriding the onScroll method, but the MapViewActivity which MapView needs, seems to be deprecated.
I am currently using GoogleMap class with SupportMapFragment, which seems not to support such a thing.
there is this question with an answer which is a bad hack and I rather not use it.
So is it possible?
Not possible without bad hacks as of 3.1.36. You may want to post a feature request on gmaps-api-issues if nobody requested that already.

Can I exploit Google's implementation of getting the user location?

Ok the answer to my question is probably rather simple so let's get to it:
Is it possible to get the user's location directly from the Google Map like the myLocationLayer does it?
To be a bit more precise: I know that there are various possibilities to get the user location and that they are explained wonderfully in answers here on SO like this one. But the Maps API v2 seems so simple and it has so many nice gettters and setters that I can't but wonder if there isn't one to just set
GoogleMap.animateCamera(CameraUpdateFactory.goToUserLocation());
and let Google handle the satellite and network part. They already have the location (you can click on the myLocationsButton and go there immediately) so I would be rather suprised if this is not possible programmatically... But I did not find anything in the API v2 tutorial.
Thanks very much for your input
You may use GoogleMap.getMyLocation(). Note that it is depracted, but IMHO it should not be.

Multiple maps v2 in TabActivity

I'm currently trying to have 2 maps v2 in different tabs of a TabActivity using MapView objects. The problem arises in the following scenario:
tab1 with a map is displayed
go to tab2 in order to display another map
in this second tab looks like the map image from first tab is overlapping the new map. See the following picture:
On the right you can see how the first map is still displayed on top of it. The touch events are going to the new map but that doesn't really help. I spent a lot of time trying to convert our app to use fragments and run into other types of issues. One of them being http://code.google.com/p/android/issues/detail?id=40035 so that's not a good option either.
So far I haven't been able to find any workaround. To easily try various changes I wrote a simple application and made it available here: https://github.com/cristizmf/TestMapsV2. It needs only the right location for maps library project and a good maps key in manifest.
Any ideas would be greatly appreciated.
Later edit: I've logged an issue for this: https://code.google.com/p/gmaps-api-issues/issues/detail?id=5027&thanks=5027&ts=1362071369
We are having the exact same problem. I suspect this is caused by the fact that the new maps uses OpenGL - possibly android cant deal with having two OpenGL widgets in the view hierarchy at the same time even though one is hidden.
The only way we worked around this was to actually remove the mapview when leaving a tab, then construct a new one and add it back in when going back into the tab. This ensures that there are never two maps constructed at once.
Nasty, but it worked.
I ran into the same issue a few days ago and was pulling my hair out over what to do.
Originally I had my app set up with "Tabs the Fragment way" as seen here to avoid the deprecated TabActivity, and used this hack to allow me to incorporate Maps v1 in with the Fragments. It basically just masks an Activity as a Fragment.
But then switched to TabActivity further down the track for simplicity / it was better suited for the whole app.
But now obviously this 2x Maps v2 fragment problem has become an issue..
So I have reverted back to "Tabs with Fragments" and just have the "hack setup" as mentioned above for each tab, which allows to have an Activity for each tab, AND having more than one Maps v2 displaying for each Tab. I haven't gone into too much depth with it, but it seems to be doing the job atm, with having just thrown some code together to test it out..
Don't know if you're still having an issue with this / this obviously isn't the most correct solution, but just thought I'd share my two cents incase you still needed it.
Cheers.

Performance of multiple MapFragments (Android Map API v2)

I searched the docs of the new Google Android Map API v2, but didn't find anything about it. Before v2 we (officially) couldn't properly display more than one map per application (process). I assume that with MapFragment implementation it's no longer an issue. I also know, that Fragments can be self-contained since API 17. And, last but not least, there is compatibility package, which makes it possible to port it all back to at least API 8 (in API 7, there's no OpenGL 2.0 so the maps wouldn't work afaik).
Sa I have all the ingredients to port my app to Maps API v2 and implement the following scenario: ViewPager contains Fragments and each of them contains a MapFragment.
But should I do it? I mean, would I face performance issues if I had e.g. 20 Fragments containing MapFragments in a ViewPager?
If you found anything that addresses my concern in the docs or somewhere else, please tell me.
EDIT: Example usage: Let's imagine a screen containing informations about some specific item - place, event, article. Also let's imagine that, among other informations, it must contain a map to be fully functional. But I would like the user to be able to swipe to another item if he wants. Each item has its own map.
Side note: I know that I can implement one map outside the ViewPager and just update it based on the currently displayed item, but it's not what I'm looking for - it's not user-centered design.
Possible solution (but not checked yet). ViewPager only keeps a few (default - 3) Fragments in it's cache and recreates them when they're about to show up, so there won't be much memory overhead as only a few MapFragments are needed. The performance problem may be the MapFragment inflation which is quite heavy, I believe - but that can be solved with recycling of MapFragments.
So why won't I implement it if I believe it will work nice? There's another problem which makes it impossible to move MapFragments without the app look like it's broken. See this.
You could easily test it but my guess is that it would very much depend on the phone as Maps still is fairly heavy to run.
That said I'd never recommend this kind of user navigation. A map can already be swiped at so why would you add ViewPager swipe as well? This would make a very confusing user experience. If you want to switch between different views/point of interests you should add buttons (overflow menu buttons preferred as in the navigation app) or something similar that helps the user navigate around.
Yes, you definitely would run into performance issues because ViewPager doesn't employ view recycling by default; you can even bloat it with ImageViews let alone MapFragments.
As far as caching and recreating fragments in ViewPager - there's a HorizontalListView lib that'd probably save you the effort as it does support view recycling.

Categories

Resources