I am developing small location based android application in which I need users current location. I am also updating users current location as soon as some change in location is occurred.My code looks like:
private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location); } public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extras) {} }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String svcName = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(svcName); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setSpeedRequired(false); criteria.setCostAllowed(true); String provider = locationManager.getBestProvider(criteria, true); //Location l = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER); Location l = locationManager.getLastKnownLocation(provider); updateWithNewLocation(l); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); } private void updateWithNewLocation(Location location) { TextView myLocationText; myLocationText = (TextView)findViewById(R.id.myLocationText); String latLongString = "No location found"; if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "Lat:" + lat + " Long:" + lng; } myLocationText.setText("Your Current Position is: " + latLongString); }
My problem is that when I use provider as network then it s working fine. But when it select gps as provider then it s giving null value. I know for the first time it gives me null value. I also used onLocationChanged method but it s still not giving me proper output. When I open my application it shows me output null value and it also start gps for serching my location. I wait for some time to get updated location but it not giving me valid output. Is there any thing wrong with my code. I am using android device.
Need Help... Thank you...