- I have an Android app that has Google Maps V2 as part of functionality.
I have
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
in my manifest, and everything else needed for maps to work.
- My app starts not on the screen with maps.
Now the question is why does my phone (Galaxy Nexus, just in case) starts showing GPS icon in status bar right when app starts, but not when I get to the screen with maps and start to work with it? I don't need to track my location and use battery power when I'm not on maps screen.
For example What's App messenger also uses GPS for its map but the icon is showed only when you open the map screen, not right on the first activity that is launched.
Googled for couple of hours but found nothing at all. Any help will be appreciated!
Edited:
MapActivity class
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.map_activity);
startGPS();
initMap();
mMapView = (MapView) findViewById(R.id.map_google_map);
mMapView.onCreate(null);
mGoogleMap = mMapView.getMap();
if (mGoogleMap != null) {
customizeGoogleMap();
loadAndFillMap();
}
}
private void startGPS() {
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
}
private void initMap() {
try {
MapsInitializer.initialize(this);
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
private void customizeGoogleMap() {
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.setMyLocationEnabled(true);
}
private void loadAndFillMap() {
new LoadAndFillMapTask().execute();
}
private class LoadAndFillMapTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
String address = Session.appData().getSelectedAddress();
mMapLocation = Api.getMapApi().getMapLocation(address);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
fillMap();
}
}
private void fillMap() {
// filling Google Map with markers etc.
}
@Override
public void onDestroy() {
super.onDestroy();
if (mMapView != null) {
mMapView.onDestroy();
}
mLocationManager.removeUpdates(mLocationListener);
}