// GET LATITUDE AND LONGITUDE BY INTERNET
LocationManager lManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
boolean netEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (netEnabled) {
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location location = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
I am trying to get lat and long using only the internet. The code above works only if I open the location. what did I have to change?
Related
I have an app that is already asking for runtime permissions to activate the localization permission and I have all ready to receive longitude and latitude. My problem now is only to get the latitude and longitude from the device itself.
This is my method, assume that the method goes to the else statement because I already have the permissions handling working
if (!checkPermissions()) {
requestPermissions()
} else {
//get latitude and long here
}
Here is a sample code
1.LocationManager & Listener
private LocationManager locationManager;
private LocationListener locationListener;
2.Setting manager & Listener
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
String locationProvider = LocationManager.GPS_PROVIDER;
currentLocation = locationManager.getLastKnownLocation(locationProvider);
if (currentLocation != null) {
double lng = currentLocation.getLongitude();
double lat = currentLocation.getLatitude();
Log.d("Log", "longtitude=" + lng + ", latitude=" + lat);
}
In my app I getting location from network and if network is not not available my code returns the last location, In both the cases I want to know whether the location is current location or it is the last known location. can somebody please show me how to know this.Thanks in advance
public Location getLocation() {
try {
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
else{ }
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
You need to achieve this using SharedPrefences
Store the current location(the latitude and longitude upto approx 5 decimal places) in shared preferences.Compare the new location with that in the shared preferences.If they are same,then the new location is your last known location,if not the new location is the new one,save it again in your shared preference.
This question already has answers here:
Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location
(7 answers)
Closed 7 years ago.
I want to get my exact latitude and longitude in android. I don't have GPS in my android device. So, I need to get my location with the help of internet.
&
If getting latitude and longitude fails, show a toast "Failed to get location"
Thanks in advance.
try Network provider
LocationManager lManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
boolean netEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (netEnabled) {
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
location = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
Old Ans
Try using this code. it will give you location from network not fro GPS
Location location =myManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
latPoint=location.getLatitude();
lngPoint=location.getLongitude();
Updated ANS
private Location getLastBestLocation() {
Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
long GPSLocationTime = 0;
if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); }
long NetLocationTime = 0;
if (null != locationNet) {
NetLocationTime = locationNet.getTime();
}
if ( 0 < GPSLocationTime - NetLocationTime ) {
return locationGPS;
}
else {
return locationNet;
}
}
You should do something like this instead of LocationManager.GPS_PROVIDER, use LocationManager.NETWORK_PROVIDER
LocationManager lManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
System.out.println("latitude network" + latitude+"longi "+location.getLongitude());
if (latitude == 0.0) {
showSettingsAlertData();
}
}
I'm trying to get GPS Longitude & Latitude.
Everything is working in case that the GPS is turning on while using any other program that turning it on.
But when i trying to use only my app , the GPS is not turning on.
here is my code:
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = m_locationManager.getBestProvider(m_c, false);
Location location = m_locationManager.getLastKnownLocation(provider);
if (location != null)
{
double lng = location.getLongitude();
double lat = location.getLatitude();
gpsLocationLon.setText("" + lng);
gpsLocationLat.setText("" + lat);
}
else
{
gpsLocationLon.setText("No Provider");
gpsLocationLat.setText("No Provider");
}
public void onLocationChanged(Location location) {
double lng = location.getLongitude();
double lat = location.getLatitude();
if(null != gpsLocationLat && null != gpsLocationLon)
{
gpsLocationLon.setText("" + lng);
gpsLocationLon.setText("" + lat);
}
}
what did i miss?
You need to start listening to location updates once you've found the best provider.
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
// ^^ This will start listening for location updates
// depending on your provider.
} else {
Log.d(LOGTAG, provider + " not enabled");
}
Remember that there are two types of providers, gps and network. So it depends on the criteria(m_c) which one is selected.
If you want to make sure that you want to listen to GPS as well as Network Updates, remove the Criteria variable and try this :
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} else {
Log.d(LOGTAG, "network provider not enabled");
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} else {
Log.d(LOGTAG, gps provider not enabled");
}
Edit 1:
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location location = m_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
else {
Log.d(LOGTAG, "network provider not enabled");
}
if (m_locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else {
Log.d(LOGTAG, "gps provider not enabled");
}
if (location != null)
{
double lng = location.getLongitude();
double lat = location.getLatitude();
gpsLocationLon.setText("" + lng);
gpsLocationLat.setText("" + lat);
}
else
{
gpsLocationLon.setText("No Provider");
gpsLocationLat.setText("No Provider");
}
public void onLocationChanged(Location location) {
double lng = location.getLongitude();
double lat = location.getLatitude();
if(null != gpsLocationLat && null != gpsLocationLon)
{
gpsLocationLon.setText("" + lng);
gpsLocationLon.setText("" + lat);
}
}
This is the code i use to find my current location but it is not pointing the location accurately, it returns a location as 9.920473,78.102423 instead of 9.909076, 78.100758
I could not figure it out where i am going wrong , please help me with suggestions.
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
}
else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Log.e("latitude", ""+latitude);
Log.e("longitude", ""+longitude);
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Log.e("latitude", ""+latitude);
Log.e("longitude", ""+longitude);
}
}
}
}
}
You are using Network Provider first, which is not very accurate. If you get a location fix from Network your code wont use GPS location. In this case, this much variation in the location is to be expected. If you need exact location, use GPS Provider.
Try with this code:
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
}