Stop using location with Google Map fragment - android

I use GM fragment in my activity. And I use checkbox to show or hide that map fragment. I want deactivate GoogleMap when it is hidden to disable using GPS. But I cant do that in my activity until it is closed. Any suggestions?
mapGoogle = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_google)).getMap();

You can use setMyLocationEnabled(boolean enabled) to enable or disable the GPS in GoogleMap.
Run
mapGoogle.setMyLocationEnabled(false);
when you want to disable the GPS, and then
mapGoogle.setMyLocationEnabled(true);
when you want to enable it again.

Related

Here maps view goes black when app go in background

I have recently started using here maps api, and i have encountered some difficulties implementing maps. When app goes in background and i come back to app, map view screen is black. I should mention that i'm using MapView and not MapFragment. MapView is inside fragment view. Beside that map is showing clearly.
mapView.setMap(mMap);
mMap = mapView.getMap();
mMap.setZoomLevel(16);
mapMarker = new MapMarker();
mapMarker.setTitle("Neki naslov");
mapMarker.setDescription("Neki opis");
mapMarker.setDraggable(true);
mapView.setMapMarkerDragListener(mOnDragListener);
mapMarker.setCoordinate(PositioningManager.getInstance().getLastKnownPosition().getCoordinate());
mMap.addMapObject(mapMarker);
mapMarker.showInfoBubble();
mMap.setCenter(PositioningManager.getInstance().getLastKnownPosition().getCoordinate(),
Map.Animation.NONE);
Most likely, you did not call MapView#onResume() and MapView#onPause() as part of the activity / fragment life cycle. If it is not called, the underlying rendering thread will not be resumed.
#Dusan, your code does not have an image for the marker, so there is no marker to show.

Google Map V21, how to programatically stop the automatic location updates?

Hello I am using a support map fragment for my Android map. Other than using default settings (no customisation), i noticed out of the box that the map seems to provide location updates automatically i.e. a dot on the map which tells where the user is and periodically updates.
My map fragment is apart of a layout whereby the map fragment visibility can be set to GONE and some other content is shown in its place.
My question is, how do i programmatically stop these location updates? I noticed when the activity which hosts the map fragment goes in a stop state, the location updates stop.
This is the behaviour i want to mimic but for when the visibility of the fragment changes.
In XML
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
Initialised in code
SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
Some settings set in onMapReady callback
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mGoogleMap.getUiSettings().setZoomControlsEnabled(false);
mGoogleMap.setOnMyLocationButtonClickListener(this);
mGoogleMap.setOnMarkerClickListener(this);
Try this code
mGoogleMap.setMyLocationEnabled(false);

Android: Google Maps API v2 rendering issue with markers and camera animation

EDIT
I've found a better STR:
Make sure to set "Do not keep activities" in Developer options in Settings.
Open the app with a SupportMapFragment as a child fragment of another fragment.
Switch to another app
Open your app again
Notice you can't interact with the map and no animations work.
Open another screen within the app
Notice there's a single frame or so of the map with the markers drawn on screen.
I have an issue with Google Maps API v2.
I am animating the camera to zoom to a set of custom marker bitmaps rendered on a MapFragment.
On selecting one of these marker tooltips I open a geo: intent (for the Google Maps app etc.)
When the user presses back it reopens my activity with the fragment back stack rebuilt.
My issue is that it doesn't render camera animations or the markers on coming back, though there is a brief display of those markers when the user presses back to go to the previous fragment.
The GoogleMap instance has the markers, but it doesn't render them - I'm guessing because the MapFragment/MapView thinks it doesn't need to be rendered.
How do I force a render of the MapView? Failing that, how do I get the MapView to recognise that the model has changed?
The issue was to do with the way I was handling my child fragments.
Every time the parent fragment would call onCreate the children fragments would get recreated.
I did the following to handle my child fragments, but there may be a better way:
private static final String TAG_FRAGMENT_MAP = "TagFragmentMap";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
if (savedInstanceState == null) {
// create the fragments for the first time
ft.add(R.id.view_flip, new SupportMapFragment(), TAG_FRAGMENT_MAP);
ft.commit();
}
}
// ...
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
mMapFragment = (SupportMapFragment)findFragmentByTag(TAG_FRAGMENT_MAP);
}

Disable my-location when user moves the map and re-enable when he press the my-location button

I'm using the setMyLocationEnabled(true) from Google Maps Android API v2 to have auto location tracking and to display the indicator just like Google Maps app. I need that the camera position follows the my-location indicator so it will be always visible, except when the user pans the map.
So, I'm using the onMyLocationChange method to move the camera to the new location, but when the user pans the map or use the search field from my app, the onMyLocationChange continues being called and the camera always comes back to the my-location coordinates.
How can I move the camera automatically when my-location is enabled, disable it when the map is manually manipulated (pan, rotate or search) and enable it back when the user press the my-location button?
Thanks.
Edit:
There is now GoogleMap.setOnMyLocationButtonClickListener, so you can skip point 1 and use that.
You can't do that with the february version of the Maps API v2. The best thing you can do is to:
have your own "go to my location" button to know when you want to start tracking
your own location change listener
a View on top of SupportMapFragment with onTouchListener (returning false!) to know when you need to stop tracking
This solution is not perfect, as it will stop tracking before user zooms on pans - it will stop when they first touch the map, but I think it is a solution that will not make any confusion for users: they touched the map, so can expect tracking to stop and can press the button to start it.
So Why don't you just why don't you just call setMyLocationEnabled(false) When user interacts with the TextView?
The only interaction you can check with the map is:
map.setOnMapClickListener(listener);
map.setOnMapLongClickListener(listener);
map.setOnMarkerClickListener(listener);
map.setOnMarkerDragListener(listener);
You could do the same thing for those interaction to disable the my-location layer.
Remove these two lines(if you have) from within the method
#Override
public void onLocationChanged(Location location) {
//Remove these two lines
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
You can detect a user gesture by using the REASON_GESTURE from the setOnCameraMoveStartedListener.
mMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
#Override
public void onCameraMoveStarted(int reason) {
if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) {
//Camera motion initiated in response to user gestures on the map.
} else if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_API_ANIMATION){
//Non-gesture animation initiated in response to user actions.
} else if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION){
//Developer initiated animation.
}
}
});
See the doc for more details: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnCameraMoveStartedListener

Destroy MapView

I have a MapActivity where you can switch between MapView (Google Maps) and OfflineMapView (my class, shows a map previously downloaded to SD card). When switching between maps I want to completely destroy and create the map views so that only one map view is in the memory. I want this for 2 reasons:
My OfflineMapView takes most of available memory for caching tiles.
Google MapView has some attached threads which I don't want running when OfflineMapView is visible.
I tried to remove the MapView from layout and null my reference to it but when I want to show it again I get an exception saying that MapActivity can have only one MapView.
EDIT:
The presence of Google MapView (visibility is set to GONE) doesn't have any effect on OfflineMapView FPS. I didn't get any OutOfMemoryErrors either.
Use ActivityGroup as your Activity class and have it start and stop sub-activities for each type of map. For example, to get the view for the Google Map:
Intent intent = new Intent(this, GoogleMapActivity.class);
Window window = getLocalActivityManager().startActivity("google-map", intent);
View googleMapView = window.getDecorView();
container.addView(googleMapView);
to destroy it:
container.removeView(googleMapView);
getLocalActivityManager().removeAllActivities();
and do the same with your offline map. This should completely stop the MapActivity and it's threads.
Note that I have found LocalActivityManager.destroyActivity() to be buggy, so I used LocalActivityManager().removeAllActivities() in the example since it does work for this case.

Categories

Resources