Android:获取onResume活动的当前位置

时间:2016-04-08 08:30:59

标签: android android-studio geolocation

我正在尝试使用GPS获取当前位置。首先,我正在检查GPS是否启用。如果GPS在此时被禁用,则显示警报并重定向用户以从“设置”启用GPS。启用GPS后,然后在onResume活动中将设置重定向到我的应用程序我获得0.00当前位置。启用GPS时,如何获得即时当前位置。

提前致谢。

 @Override
    protected void onResume() {
        super.onResume();
        getCurrenLocation();
    }


public void getCurrenLocation() {
        gps = new GpsTracker(GetLocation.this);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//        boolean gps_enabled = false;
//        boolean network_enabled = false;

        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }

        try {
            network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }
        if (gps_enabled && network_enabled) {

            if (gps.canGetLocation()) {
                latitude = gps.getLatitude();
                longitude = gps.getLongitude();
                // Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                if (latitude != 0.0 && longitude != 0.0) {
                    txtLocation.setVisibility(View.VISIBLE);
                    txtLong.setVisibility(View.VISIBLE);
                    txtLat.setVisibility(View.VISIBLE);
                    txtLocation.setText("Your Location is - \nLat: " + latitude + "\nLong: " + longitude);
                    txtLong.setText(Double.toString(longitude));
                    txtLat.setText(Double.toString(latitude));
                }

                Log.d("latitude onResume:: :: :: ::", String.valueOf(latitude));
                Log.d("longitude onResume:: :: :: ::", String.valueOf(longitude));
            }
        } else if (!gps_enabled && !network_enabled) {
            alertDialog();
        }
    }

2 个答案:

答案 0 :(得分:1)

您是否已将权限添加到清单中?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我认为启用GPS后无法立即获取该位置。 GPS需要修复歧义并估算您的位置。 如果你需要一个位置,你可以在粗略的位置。但是,这个位置比GPS更不准确。

答案 1 :(得分:-1)

尝试使用此课程获取纬度和经度

import android.app.AlertDialog.Builder;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;

 public class GPSTracker extends Service implements LocationListener {


 private Context context;

 boolean isGPSEnabled=false;
 boolean isNetworkEnabled=false;
 boolean canGetLocation=false;

 Location location;

 double latitude;
 double longitude;

 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10;
 private static final long MIN_TIME_BW_UPDATES=1000*60*1;

 protected LocationManager locationManager;

 public GPSTracker(Context context)
  {
 this.context=context;
 getLocation();
   }

  public Location getLocation()
  {
  try{
   locationManager=(LocationManager)     context.getSystemService(LOCATION_SERVICE);
  isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);




if(!isGPSEnabled && !isNetworkEnabled)
{
    showSettingsAlert();
}
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();
    }
    }
    }

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

            }

        }
    }


}


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

 public void stopUsingGPS()
  {
   if(locationManager !=null)
    {
     locationManager.removeUpdates(GPSTracker.this);
    }
  }

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

 }


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

}

public boolean canGetLocation(){

 return this.canGetLocation;

  }

  public void showSettingsAlert(){
  Builder alertDialog=new Builder(context);
 alertDialog.setTitle("GPS Settings");
 alertDialog.setMessage("GPS is not enabled.Do you want to go to the settings menu?");
 alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

     @Override
    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() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();

    }
});
alertDialog.show();
}


@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

 }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
  // TODO Auto-generated method stub

 }

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
 }


 }

然后

GPSTracker gps;

public void getCurrenLocation() {
    gps = new GPSTracker(classname.this);

    if (gps.canGetLocation) {
        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();
                }
}

希望这有帮助