I have some trouble understanding the concept of the Maps API in Android. Is it correct that every time I want to draw something new on the map, I create a new Overlay? This seems highly inefficient at a first glance. Is the whole layer structure baked down and I dont have to worry about performance at all or am I missing something like updateOverlay?
Or would I just implement my own updateOverlay() method when I extend the Overlay class and call MapView.invalidate() afterwards?
You create an overlay only once and the add it to the MapView. Then you need to call postInvalidate() on the MapView, so the new overlay will appear on the screen. Without this call, the overlay will be created, but will not show up.
Since the onResume() method in the activity lifecycle is always called when application starts or just wake up, you want to enable location tracking in onResume() and disable it in onPause(). There is a helpful method in MyLocationOverlay class: runOnFirstFix() This method allows you to set a code that will run as soon as we have location at all:
#Override
public void onResume()
{
super.onResume();
whereAmI.enableMyLocation();
whereAmI.runOnFirstFix( new Runnable() {
public void run() {
mapController.setCenter(whereAmI.getMyLocation());
}
});
}
After that, you don't need to do anything yourself, because the MyLocationOverlay is getting location updates and putting the blinking blue dot in that location.
#Override
public void onPause()
{
super.onPause();
whereAmI.disableMyLocation();
}
Related
I want to update the value of a marker when the maps stop moving, but I canĀ“t detect it. Does someone know how?. I already tried with the PanGestureRecognizer
Implement the GoogleMap.IOnCameraIdleListener and assign it to your map instance (assumably in the MapReady callback):
Implement GoogleMap.IOnCameraIdleListener
public void OnCameraIdle()
{
// a callback that is invoked when camera movement has ended.
}
Assign the Idle listener to the GoogleMap instance:
googleMap.SetOnCameraIdleListener(this);
Re: setOnCameraIdleListener
I need to listen CameraPosition changes to draw a custom compass. The problem that: GoogleMap.OnCameraChangeListener onCameraChange
this listener may not be notified of intermediate camera positions.
it fires with random delays (can not understand why)
Is there are any way to listen to CameraPosition bearing changes? (In ios f.e. it's possible to achieve using Key-Value Observing), reflection...?
Thanks.
Put FrameLayout above map and catch touches:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mCatchTouchFrameLayoutListener != null)
mCatchTouchFrameLayoutListener.onTouch(ev);
return false;
}
To move the camera instantly with the given CameraUpdate, you can call GoogleMap.moveCamera(CameraUpdate).
You can make the user experience more pleasing, especially for short moves, by animating the change. To do this instead of calling GoogleMap.moveCamera() call GoogleMap.animateCamera(). The map will move smoothly to the new attributes. The most detailed form of this method, GoogleMap.animateCamera(cameraUpdate, duration, callback), offers three arguments:
CameraUpdate: The CameraUpdate describing where to move the camera.
Callback: An object that implements GoogleMap.CancellableCallback. This generalized interface for handling tasks defines two methods onCancel() and onFinished(). For animation, the methods are called in the following circumstances:
onFinish()
Invoked if the animation goes to completion without interruption.
onCancel()
Invoked if the animation is interrupted by calling stopAnimation() or starting a new camera movement.
Alternatively, this can also occur if you call GoogleMap.stopAnimation().
Duration: Desired duration of the animation, in milliseconds, as an int.
I'am having troubles with Googles MapView Activity.
The reason for my problem:
My application relies on many asynchronous events. When I start an Activity containing the Map, I've a template mechanism which adds and initializes the Map and relays all activity callbacks like onCreate(), onResume() to the map. All works fine!
But when this activity is restored from background my template mechanism is not yet ready to create the dynamic layout in onCreate(), hence I can't relay the lifecycle callbacks which are needed for the Map. In worst case, the Map is created after onResume().
This simplified code is for understanding the issue:
public class MapActivity{
List<Templates> templates;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (app.isInit())) {
initActivity();
}
templates.onCreate();
}
// init after templates are loaded
private void initActivity() {
templates = templateEngine.renderSomeTemplates()
}
#Override
protected void onResume() {
super.onResume();
templates.onResume()
}
public void onEventMainThread(InitAppEvent event) {
initActivity()
}
}
So my question.
Is there any way to add a google MapView programmatically during runtime without passing onCreate() and so on to it or do some lazy init?
Ok, I figured it out.
Apparently it's impossible to live without the callbacks as described in the docs:
http://developer.android.com/reference/com/google/android/gms/maps/MapView.html
So I found a trick to force my activity to start all over again, as soon as my dependencies (templates) are ready.
If anyone is looking for a neat way to restart an activity: there is recreate()
http://developer.android.com/reference/android/app/Activity.html#recreate()
It basically finishes the activity and repeats the normal lifecycle from the beginning.
Yes its is possible, take a look at the samples in the SDK
I have created an application that has a 'geolocation' feature responsible for spotting a user on the Google map like many other applicatons. I used "LocationManager.NETWORK_PROVIDER" to locate the user and at the same time I instantiate and start "MyLocationOverlay" (in the onLocationChanged() method) to get the location. Because of the second one, the GPS turns on (blinking on the top) which is OK.
The problem is, after the application is closed (back button or through task manager), the GPS feature is still hanging there, trying to get the updates.
How to turn it off after the user leaves the activity? I tried suggestions from here and other forums like putting locationManager.removeUpdates(this); and locationManager.removeUpdates(mMyLocationOverlay); within the methods onPause(), onStop(), onDestroy(). The method OnPause looks like this:
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
locationManager.removeUpdates(mMyLocationOverlay);
}
('this' references my class that implements LocationListener)
Please, can someone help me to turn off GPS updates after leaving the activity (it's a class that extends MapActivity) but not turn off the GPS feature on the phone itself?
Interesting thing is that when I remove the part with MyLocationOverlay, GPS will not start of course and therefore no problem. So I'm pretty sure that mMyLocationOverlay is the listener that "won't stop" and producing a problem.
googleMap.setMyLocationEnabled(false); in onPause() solved my issue. And I'm setting that to true in OnCreate(). Hope this may help others.
If you want to close (or end) the application you can use
System.exit(0);
so when the application is closed, all the services you use will close.
Set your LocationListener equal to null and re-instantiate onResume()
I have just started working on my first Android application and am going ok. I have managed to get the app to locate my current position on a map and place the blue circle there. I can see the satellite icon in the notification bar working (satellite dish with rays coming off it).
I am very new to android phones altogether but upon being done with my app I just exit it by either using the back key or the home key which works fine, however I notice the satellite icon is still working. Going back to my phone an hour later the GPS is still running. This of course sends the phone battery flat very quickly.
I assume unlike iPhone that Android apps can run in the background and this is still running. How do I get my app to stop using GPS when it is no longer on the map view?
Here is an example of what I have done so far:
//find and initialise map view
private void initMapView() {
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(false);
map.setBuiltInZoomControls(true);
}
//start tracking the position on the map
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
//overlay.enableCompass(); does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
//zoom in to current location
controller.setZoom(8);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
Do I need to disable something when I leave the map view? if so, how?
Cheers guys
You should probably take a look at the Activity Lifecycle. In short, your assumption is correct: Android does not just kill your App when you leave it.
You'll have to implement the onPause() method and unregister the GPS listener. And maybe also remove the mapview, but I don't think that matters much in terms of battery life.
Also, you should move the registration of the GPS into the onResume() method. That's necessary in order to enable the GPS again when your App coms back into view.
The onPause(), onResume(), onCreate() (and so on) methods get called by android itself at the appropriate times.
Daniel,
Within the maps overlay API there are function calls that will register and unregister the listener for you...
You are already calling the enableMyLocation(); now you need to call overlay.disableMyLocation();
In the onPause.