how to get gps location android

前端 未结 5 1479
有刺的猬
有刺的猬 2020-12-08 17:32

I am trying to have a constant gps listener that will send its location (long and lat coordinates) to a web server every x mins. On a button click it will also send its loca

5条回答
  •  广开言路
    2020-12-08 18:08

    this code works for me (api 17)

    try {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    
        lat = location.getLatitude();
            lon = location.getLongitude();
    
    
        }
    }
    catch (Exception e){
        e.printStackTrace();
    }
    

提交回复
热议问题