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);
Related
I am struggling with animateCamera() method of the Google Maps. It only works on first launch of activity and if the activity is destroyed and created again, camera animations doesn't work but map is loaded fine. I have tried debugging the code, everything gets executed but map doesn't animate without any error or log. Although animation works when same mapFragment is used in the fragment but in activity it doesn't seem to work.
The solution mentioned in this this question is deprecated now and I am unable to fix this issue.
Map Fragment
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mapFragment.setRetainInstance(true);
#Override
public void onMapReady(GoogleMap googleMap) {
if (mMap == null) mMap = googleMap;
}
Code for animating camera
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, ZOOM_LEVEL));
Making entire project again from old code fixed the issue. But this issue re-appeared in another app. I found out that application was using old reference to mMap. This can be fixed by either getting reference from fragment manager or just setting mMap = null when activity is destroyed.
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.
I have fragment for map in XML file as below,
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" >
</fragment>
In which activity I am displaying the Google map, that is not a main activity. I am redirecting to map activity from my main activity on button click. There is no any problem with map. But when I click on button of main activity then the process stops for sometime and suddenly black screen arrives in front and after sometime, map activity displays the map. It is not expected behaviour of application. Is there any way to load that map after map activity loaded? I mean loading map in background?
Use getMapAsync() (introduced in Google Play Services v6.5.87) instead of getMap() then Implement OnMapReadyCallback on your fragment:
#Override
public void onMapReady(GoogleMap map) {
// do something with map
}
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.
I have a fragment activity as seen in my last question:
App crashing when using fragments
It crushes because of a GoogleMap inside one of my fragments (MainActivity).
right now thats the code:
private GoogleMap map;
map = ((MapFragment) getActivity().getFragmentManager() .findFragmentById(R.id.map)).getMap();
I cant change GoogleMap. it causes other problems in my code as im using it.
What should I do?
Thanks for your assistance!
Ive tried to relocate that piece of code to onCreateView. Doesn't work either.