首页 > 解决方案 > 如何获取设备的gps纬度和经度?

问题描述

有人请建议一个代码来获取android中的android设备的gps 位置(纬度和经度) 。我尝试了很多例子,但在

Location location = locationManager.getLastKnownLocation(bestProvider);

得到location=null.
我在下面发布我的代码。

public class MainActivity extends AppCompatActivity {


Geocoder geocoder;
String bestProvider;
List<Address> user = null;
double lat,lng;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    bestProvider = lm.getBestProvider(criteria, false);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = lm.getLastKnownLocation(bestProvider);

    if (location == null)
    {
        Toast.makeText(this,"Location Not found",Toast.LENGTH_LONG).show();
    }
    else{
        geocoder = new Geocoder(this);
        try
        {
            user = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            lat=(double)user.get(0).getLatitude();
            lng=(double)user.get(0).getLongitude();
            System.out.println(" DDD lat: " +lat+",  longitude: "+lng);

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


}

标签: androidgps

解决方案


you can try with the LocationManager.

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

then you LocationListener which will give you asynchronous updates of your location.

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
    longitude = location.getLongitude();
    latitude = location.getLatitude();
  }
}

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

also add permissions into your manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.INTERNET" />

I hope this will help you.


推荐阅读