I have an android app which uses google map and has a feature to show the current location. I have received few comments saying that “the current location is not coming up even if the GPS is on.” I tried it with couple of devices but I could not see the issue but yesterday I faced the issue on my phone too where it was working fine earlier.
The solution that I figured out is if the current location is not coming up then turn off the wi-fi and use mobile internet for getting the current location and it started showing up. I know it seems little weird but it worked for me. Please suggest me the changes needed in my code, if I am missing something. I can’t suggest the above workaround for my app users.
locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
cEnabled = getIntent().getExtras().getBoolean("cEnabled");
if (!enabled && !cEnabled) {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Would you like to activate GPS?\n(recommended- Yes)")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Activate GPS?");
// Icon for AlertDialog
alert.setIcon(R.drawable.location1);
alert.show();
}
ImageButton clocButton = (ImageButton) findViewById(R.id.cLoc);
clocButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
getToMyLocation();
return false;
}
});
private void getToMyLocation() {
mapView.invalidate();
Criteria criteria = new Criteria();
String provider = locMgr.getBestProvider(criteria, false);
Location lastLoc = locMgr.getLastKnownLocation(provider);
if(lastLoc != null) {
GeoPoint lastPoint = new GeoPoint((int)(lastLoc.getLatitude()*1E6), (int)(lastLoc.getLongitude()*1E6));
center = lastPoint;
showLocation(lastPoint);
}
else
{
Toast.makeText(mContext, "Waiting for current location", Toast.LENGTH_SHORT).show();
}
myLocOverlay.enableMyLocation();
myLocOverlay.runOnFirstFix(new Runnable() {
public void run() {
GeoPoint loc = myLocOverlay.getMyLocation();
mapView.getController().setCenter(loc);
center = loc;
}
});
}
private void showLocation(GeoPoint location) {
if (location != null) {
myLocOverlay.enableMyLocation();
mapView.getController().animateTo(location);
}
}
I have added following permissions in manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
I have used the Android Developers’ Making Your App Location-Aware tutorial before and it has worked fine for me. Have you looked through this and compared it to your own code?
You may see something you are missing, or a better way of implementing parts of it, which may clear up these issues.