Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locating current position in android without GPS

I want to develop an application in which, if i open the app, it will show current location of my device. I want to do it without using gps. I have write a code for it. But it will show United States location while opening the map. I want it to show my current location (i.e Pune, India). How do i get my approximate correct location?

Here is my code

protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.main);

    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

      LocationListener locListener = new LocationListener(){
          public void onLocationChanged(Location loc)
          {

            loc.getLatitude();
            loc.getLongitude();

            String Text = "My current location is: " +
            "Latitud = " + loc.getLatitude() +
            "Longitud = " + loc.getLongitude();

            Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
          }
like image 537
PrasadM Avatar asked Jan 15 '13 08:01

PrasadM


2 Answers

Here is MyLocation Listener to Get Current Location without GPS, Using Network Provider

public class MyLocationListener extends Service implements LocationListener {

    private static final String TAG = "MyLocationListener";

    private Context context = null;
    private Location location = null;
    private LocationManager locationManager = null;

    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;

    public double latitude = 0.0;
    public double longitude = 0.0;

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    public MyLocationListener(Context ctx) {
        Log.v(TAG + ".MyLocationListener",
                "MyLocationListener constructor called");
        this.context = ctx;
        getLocationValue();
    }

    public Location getLocationValue() {
        Log.v(TAG + ".getLocationValue", "getLocationValue method called");

        try {
            locationManager = (LocationManager) context
                    .getSystemService(LOCATION_SERVICE);

            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (isNetworkEnabled) {

                // Toast.makeText(context, "Net", 1).show();
                Log.v(TAG + ".getLocationValue", "Network provider enabled");
                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.v(TAG, "Co-ordinates are: " + latitude + " "+ longitude);

                    }
                }

            } else {
                showSettingsAlert();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    /**
     * Stop using GPS listener Calling this function will stop using GPS in your
     * app
     * */
    public void stopUsingGPS() {
        if (locationManager != null) {
            locationManager.removeUpdates(MyLocationListener.this);
        }
    }

    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     * 
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog On pressing Settings button will
     * lauch Settings Options
     * */
    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

        alertDialog.setTitle("GPS Settings");
        alertDialog
                .setMessage("GPS is not enabled. Do you want to go to settings menu?");

        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        context.startActivity(intent);
                    }
                });
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

Just call above code whenever you need current location co-ordinates this way

MyLocationListener mylistner = new MyLocationListener(context);
        double lat = mylistner.latitude;
        double lon = mylistner.longitude;
like image 82
swiftBoy Avatar answered Nov 15 '22 00:11

swiftBoy


You should request 'mlocManager' to locate, and set its listener:

public void getCurrentLocation()
{
    if (mlocManager != null) {
        mlocManager .requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locListener );
        mlocManager .requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locListener );
    }

}
like image 41
noam Avatar answered Nov 14 '22 22:11

noam