-1

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.

Hello World
  • 84
  • 2
  • 10

3 Answers3

1

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();

                        }
                }
Dilavar Malek
  • 1,157
  • 11
  • 18
1

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;
}
}
Umair Khalid
  • 2,259
  • 1
  • 21
  • 28
  • Hello Umair Khalid. Thanks for your answer. What can I do to configure : If GPS is available then get location from GPS else use network. Thanks Again. – Hello World Apr 04 '15 at 12:11
  • getLastKnownLocation() stores the best last location. and if you do not change your location to next point then it will give you the last known location it stores. Cheers! – Umair Khalid Apr 23 '15 at 06:13
0

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();
                                }
                            }
George Thomas
  • 4,566
  • 5
  • 30
  • 65