Android:在onResume活动中获取当前位置

Android:在onResume活动中获取当前位置,android,android-studio,geolocation,Android,Android Studio,Geolocation,我正在尝试使用GPS获取当前位置。所以首先我要检查GPS是否启用。如果此时禁用GPS,am将显示警报,并重定向用户以从设置启用GPS。启用GPS后,将设置重定向到我的应用程序,此时在onResume活动中,我将获得0.00的当前位置。启用GPS时,如何获取即时当前位置 提前谢谢 @Override protected void onResume() { super.onResume(); getCurrenLocation(); } publ

我正在尝试使用GPS获取当前位置。所以首先我要检查GPS是否启用。如果此时禁用GPS,am将显示警报,并重定向用户以从设置启用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();
        }
    }

尝试此类获取纬度和经度

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

希望这有帮助

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

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

我认为启用GPS后不可能立即获得位置。GPS需要修正模糊性并估计您的位置。
如果您需要一个位置,您可以立即选择粗略的位置。但是,这个位置比GPS要精确得多。

@komal根据供应商和地点,获取位置总是需要时间。GPS从min 3卫星接收信号,do使用三边测量,因此根据信号强度和您的位置,这可能需要时间。甚至谷歌地图应用程序有时也需要10秒才能获得Lat/Lng。你应该使用谷歌播放服务


尝试此演示

“启用GPS时如何获取即时当前位置”您不能。GPS技术不是这样工作的。您必须请求位置并等待更新。这不是即时的,您需要一个LocationListener,否则您将无法获得任何更新。你找到一个好的万无一失的解决方案了吗?我正在寻找类似的东西。谢谢,但当我在“onResume”上调用getCurrenLocation()时,它仍然是0.0。你在设备上尝试过吗?模拟器有时不会显示gps坐标是的,它没有进入设备。人们真的需要停止传播这种“GPSTracker”这是一个非常糟糕的代码示例,多年来,它一直是世界各地许多问题的根源。太糟糕了。请拯救人类,停止传播坏建议。是的,我已经加对了!!获取Latlong即时位置需要时间您只能获取最后一个已知位置,或者运行服务并请求位置更新到10米100毫秒,但这会耗尽您的电池电量。